xref: /OK3568_Linux_fs/kernel/drivers/mmc/host/dw_mmc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Synopsys DesignWare Multimedia Card Interface driver
4*4882a593Smuzhiyun  *  (Based on NXP driver for lpc 31xx)
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2009 NXP Semiconductors
7*4882a593Smuzhiyun  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/blkdev.h>
11*4882a593Smuzhiyun #include <linux/clk.h>
12*4882a593Smuzhiyun #include <linux/debugfs.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/dma-mapping.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/iopoll.h>
19*4882a593Smuzhiyun #include <linux/ioport.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/of_address.h>
22*4882a593Smuzhiyun #include <linux/platform_device.h>
23*4882a593Smuzhiyun #include <linux/pm_runtime.h>
24*4882a593Smuzhiyun #include <linux/seq_file.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/stat.h>
27*4882a593Smuzhiyun #include <linux/delay.h>
28*4882a593Smuzhiyun #include <linux/irq.h>
29*4882a593Smuzhiyun #include <linux/mmc/card.h>
30*4882a593Smuzhiyun #include <linux/mmc/host.h>
31*4882a593Smuzhiyun #include <linux/mmc/mmc.h>
32*4882a593Smuzhiyun #include <linux/mmc/sd.h>
33*4882a593Smuzhiyun #include <linux/mmc/sdio.h>
34*4882a593Smuzhiyun #include <linux/bitops.h>
35*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
36*4882a593Smuzhiyun #include <linux/of.h>
37*4882a593Smuzhiyun #include <linux/of_gpio.h>
38*4882a593Smuzhiyun #include <linux/mmc/slot-gpio.h>
39*4882a593Smuzhiyun #include <linux/soc/rockchip/rk_sdmmc.h>
40*4882a593Smuzhiyun #include <linux/soc/rockchip/rockchip_decompress.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "dw_mmc.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /* Common flag combinations */
45*4882a593Smuzhiyun #define DW_MCI_DATA_ERROR_FLAGS	(SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
46*4882a593Smuzhiyun 				 SDMMC_INT_HTO | SDMMC_INT_SBE  | \
47*4882a593Smuzhiyun 				 SDMMC_INT_EBE | SDMMC_INT_HLE)
48*4882a593Smuzhiyun #define DW_MCI_CMD_ERROR_FLAGS	(SDMMC_INT_RTO | SDMMC_INT_RCRC | \
49*4882a593Smuzhiyun 				 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
50*4882a593Smuzhiyun #define DW_MCI_ERROR_FLAGS	(DW_MCI_DATA_ERROR_FLAGS | \
51*4882a593Smuzhiyun 				 DW_MCI_CMD_ERROR_FLAGS)
52*4882a593Smuzhiyun #define DW_MCI_SEND_STATUS	1
53*4882a593Smuzhiyun #define DW_MCI_RECV_STATUS	2
54*4882a593Smuzhiyun #define DW_MCI_DMA_THRESHOLD	16
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define DW_MCI_FREQ_MAX	200000000	/* unit: HZ */
57*4882a593Smuzhiyun #define DW_MCI_FREQ_MIN	100000		/* unit: HZ */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define IDMAC_INT_CLR		(SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
60*4882a593Smuzhiyun 				 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
61*4882a593Smuzhiyun 				 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
62*4882a593Smuzhiyun 				 SDMMC_IDMAC_INT_TI)
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define DESC_RING_BUF_SZ	PAGE_SIZE
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun struct idmac_desc_64addr {
67*4882a593Smuzhiyun 	u32		des0;	/* Control Descriptor */
68*4882a593Smuzhiyun #define IDMAC_OWN_CLR64(x) \
69*4882a593Smuzhiyun 	!((x) & cpu_to_le32(IDMAC_DES0_OWN))
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	u32		des1;	/* Reserved */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	u32		des2;	/*Buffer sizes */
74*4882a593Smuzhiyun #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
75*4882a593Smuzhiyun 	((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
76*4882a593Smuzhiyun 	 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	u32		des3;	/* Reserved */
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	u32		des4;	/* Lower 32-bits of Buffer Address Pointer 1*/
81*4882a593Smuzhiyun 	u32		des5;	/* Upper 32-bits of Buffer Address Pointer 1*/
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	u32		des6;	/* Lower 32-bits of Next Descriptor Address */
84*4882a593Smuzhiyun 	u32		des7;	/* Upper 32-bits of Next Descriptor Address */
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun struct idmac_desc {
88*4882a593Smuzhiyun 	__le32		des0;	/* Control Descriptor */
89*4882a593Smuzhiyun #define IDMAC_DES0_DIC	BIT(1)
90*4882a593Smuzhiyun #define IDMAC_DES0_LD	BIT(2)
91*4882a593Smuzhiyun #define IDMAC_DES0_FD	BIT(3)
92*4882a593Smuzhiyun #define IDMAC_DES0_CH	BIT(4)
93*4882a593Smuzhiyun #define IDMAC_DES0_ER	BIT(5)
94*4882a593Smuzhiyun #define IDMAC_DES0_CES	BIT(30)
95*4882a593Smuzhiyun #define IDMAC_DES0_OWN	BIT(31)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	__le32		des1;	/* Buffer sizes */
98*4882a593Smuzhiyun #define IDMAC_SET_BUFFER1_SIZE(d, s) \
99*4882a593Smuzhiyun 	((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	__le32		des2;	/* buffer 1 physical address */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	__le32		des3;	/* buffer 2 physical address */
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* Each descriptor can transfer up to 4KB of data in chained mode */
107*4882a593Smuzhiyun #define DW_MCI_DESC_DATA_LENGTH	0x1000
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_CPU_RV1106)
110*4882a593Smuzhiyun static spinlock_t *g_sdmmc_ispvicap_lock;
111*4882a593Smuzhiyun 
rv1106_sdmmc_get_lock(void)112*4882a593Smuzhiyun void rv1106_sdmmc_get_lock(void)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	if (g_sdmmc_ispvicap_lock)
115*4882a593Smuzhiyun 		spin_lock(g_sdmmc_ispvicap_lock);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun EXPORT_SYMBOL(rv1106_sdmmc_get_lock);
118*4882a593Smuzhiyun 
rv1106_sdmmc_put_lock(void)119*4882a593Smuzhiyun void rv1106_sdmmc_put_lock(void)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	if (g_sdmmc_ispvicap_lock)
122*4882a593Smuzhiyun 		spin_unlock(g_sdmmc_ispvicap_lock);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun EXPORT_SYMBOL(rv1106_sdmmc_put_lock);
125*4882a593Smuzhiyun #endif
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #if defined(CONFIG_DEBUG_FS)
dw_mci_req_show(struct seq_file * s,void * v)128*4882a593Smuzhiyun static int dw_mci_req_show(struct seq_file *s, void *v)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct dw_mci_slot *slot = s->private;
131*4882a593Smuzhiyun 	struct mmc_request *mrq;
132*4882a593Smuzhiyun 	struct mmc_command *cmd;
133*4882a593Smuzhiyun 	struct mmc_command *stop;
134*4882a593Smuzhiyun 	struct mmc_data	*data;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* Make sure we get a consistent snapshot */
137*4882a593Smuzhiyun 	spin_lock_bh(&slot->host->lock);
138*4882a593Smuzhiyun 	mrq = slot->mrq;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (mrq) {
141*4882a593Smuzhiyun 		cmd = mrq->cmd;
142*4882a593Smuzhiyun 		data = mrq->data;
143*4882a593Smuzhiyun 		stop = mrq->stop;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		if (cmd)
146*4882a593Smuzhiyun 			seq_printf(s,
147*4882a593Smuzhiyun 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
148*4882a593Smuzhiyun 				   cmd->opcode, cmd->arg, cmd->flags,
149*4882a593Smuzhiyun 				   cmd->resp[0], cmd->resp[1], cmd->resp[2],
150*4882a593Smuzhiyun 				   cmd->resp[2], cmd->error);
151*4882a593Smuzhiyun 		if (data)
152*4882a593Smuzhiyun 			seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
153*4882a593Smuzhiyun 				   data->bytes_xfered, data->blocks,
154*4882a593Smuzhiyun 				   data->blksz, data->flags, data->error);
155*4882a593Smuzhiyun 		if (stop)
156*4882a593Smuzhiyun 			seq_printf(s,
157*4882a593Smuzhiyun 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
158*4882a593Smuzhiyun 				   stop->opcode, stop->arg, stop->flags,
159*4882a593Smuzhiyun 				   stop->resp[0], stop->resp[1], stop->resp[2],
160*4882a593Smuzhiyun 				   stop->resp[2], stop->error);
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	spin_unlock_bh(&slot->host->lock);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(dw_mci_req);
168*4882a593Smuzhiyun 
dw_mci_regs_show(struct seq_file * s,void * v)169*4882a593Smuzhiyun static int dw_mci_regs_show(struct seq_file *s, void *v)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct dw_mci *host = s->private;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	pm_runtime_get_sync(host->dev);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
176*4882a593Smuzhiyun 	seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
177*4882a593Smuzhiyun 	seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
178*4882a593Smuzhiyun 	seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
179*4882a593Smuzhiyun 	seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
180*4882a593Smuzhiyun 	seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(host->dev);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(dw_mci_regs);
187*4882a593Smuzhiyun 
dw_mci_init_debugfs(struct dw_mci_slot * slot)188*4882a593Smuzhiyun static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	struct mmc_host	*mmc = slot->mmc;
191*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
192*4882a593Smuzhiyun 	struct dentry *root;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	root = mmc->debugfs_root;
195*4882a593Smuzhiyun 	if (!root)
196*4882a593Smuzhiyun 		return;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops);
199*4882a593Smuzhiyun 	debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops);
200*4882a593Smuzhiyun 	debugfs_create_u32("state", S_IRUSR, root, &host->state);
201*4882a593Smuzhiyun 	debugfs_create_xul("pending_events", S_IRUSR, root,
202*4882a593Smuzhiyun 			   &host->pending_events);
203*4882a593Smuzhiyun 	debugfs_create_xul("completed_events", S_IRUSR, root,
204*4882a593Smuzhiyun 			   &host->completed_events);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun #endif /* defined(CONFIG_DEBUG_FS) */
207*4882a593Smuzhiyun 
dw_mci_ctrl_reset(struct dw_mci * host,u32 reset)208*4882a593Smuzhiyun static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	u32 ctrl;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	ctrl = mci_readl(host, CTRL);
213*4882a593Smuzhiyun 	ctrl |= reset;
214*4882a593Smuzhiyun 	mci_writel(host, CTRL, ctrl);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* wait till resets clear */
217*4882a593Smuzhiyun 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
218*4882a593Smuzhiyun 				      !(ctrl & reset),
219*4882a593Smuzhiyun 				      1, 500 * USEC_PER_MSEC)) {
220*4882a593Smuzhiyun 		dev_err(host->dev,
221*4882a593Smuzhiyun 			"Timeout resetting block (ctrl reset %#x)\n",
222*4882a593Smuzhiyun 			ctrl & reset);
223*4882a593Smuzhiyun 		return false;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return true;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
dw_mci_wait_while_busy(struct dw_mci * host,u32 cmd_flags)229*4882a593Smuzhiyun static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	u32 status;
232*4882a593Smuzhiyun 	u32 delay = 10;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	/*
235*4882a593Smuzhiyun 	 * Databook says that before issuing a new data transfer command
236*4882a593Smuzhiyun 	 * we need to check to see if the card is busy.  Data transfer commands
237*4882a593Smuzhiyun 	 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
238*4882a593Smuzhiyun 	 *
239*4882a593Smuzhiyun 	 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
240*4882a593Smuzhiyun 	 * expected.
241*4882a593Smuzhiyun 	 */
242*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
243*4882a593Smuzhiyun 	if (host->slot->mmc->caps2 & MMC_CAP2_NO_SD &&
244*4882a593Smuzhiyun 	    host->slot->mmc->caps2 & MMC_CAP2_NO_SDIO)
245*4882a593Smuzhiyun 		delay = 0;
246*4882a593Smuzhiyun #endif
247*4882a593Smuzhiyun 	if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
248*4882a593Smuzhiyun 	    !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
249*4882a593Smuzhiyun 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
250*4882a593Smuzhiyun 					      status,
251*4882a593Smuzhiyun 					      !(status & SDMMC_STATUS_BUSY),
252*4882a593Smuzhiyun 					      delay, 500 * USEC_PER_MSEC))
253*4882a593Smuzhiyun 			dev_err(host->dev, "Busy; trying anyway\n");
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
mci_send_cmd(struct dw_mci_slot * slot,u32 cmd,u32 arg)257*4882a593Smuzhiyun static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
260*4882a593Smuzhiyun 	unsigned int cmd_status = 0;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	mci_writel(host, CMDARG, arg);
263*4882a593Smuzhiyun 	wmb(); /* drain writebuffer */
264*4882a593Smuzhiyun 	dw_mci_wait_while_busy(host, cmd);
265*4882a593Smuzhiyun 	mci_writel(host, CMD, SDMMC_CMD_START | cmd);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
268*4882a593Smuzhiyun 				      !(cmd_status & SDMMC_CMD_START),
269*4882a593Smuzhiyun 				      1, 500 * USEC_PER_MSEC))
270*4882a593Smuzhiyun 		dev_err(&slot->mmc->class_dev,
271*4882a593Smuzhiyun 			"Timeout sending command (cmd %#x arg %#x status %#x)\n",
272*4882a593Smuzhiyun 			cmd, arg, cmd_status);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
dw_mci_prepare_command(struct mmc_host * mmc,struct mmc_command * cmd)275*4882a593Smuzhiyun static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
278*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
279*4882a593Smuzhiyun 	u32 cmdr;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	cmd->error = -EINPROGRESS;
282*4882a593Smuzhiyun 	cmdr = cmd->opcode;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (cmd->opcode == MMC_STOP_TRANSMISSION ||
285*4882a593Smuzhiyun 	    cmd->opcode == MMC_GO_IDLE_STATE ||
286*4882a593Smuzhiyun 	    cmd->opcode == MMC_GO_INACTIVE_STATE ||
287*4882a593Smuzhiyun 	    (cmd->opcode == SD_IO_RW_DIRECT &&
288*4882a593Smuzhiyun 	     ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
289*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_STOP;
290*4882a593Smuzhiyun 	else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
291*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
294*4882a593Smuzhiyun 		u32 clk_en_a;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 		/* Special bit makes CMD11 not die */
297*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_VOLT_SWITCH;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		/* Change state to continue to handle CMD11 weirdness */
300*4882a593Smuzhiyun 		WARN_ON(slot->host->state != STATE_SENDING_CMD);
301*4882a593Smuzhiyun 		slot->host->state = STATE_SENDING_CMD11;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		/*
304*4882a593Smuzhiyun 		 * We need to disable low power mode (automatic clock stop)
305*4882a593Smuzhiyun 		 * while doing voltage switch so we don't confuse the card,
306*4882a593Smuzhiyun 		 * since stopping the clock is a specific part of the UHS
307*4882a593Smuzhiyun 		 * voltage change dance.
308*4882a593Smuzhiyun 		 *
309*4882a593Smuzhiyun 		 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
310*4882a593Smuzhiyun 		 * unconditionally turned back on in dw_mci_setup_bus() if it's
311*4882a593Smuzhiyun 		 * ever called with a non-zero clock.  That shouldn't happen
312*4882a593Smuzhiyun 		 * until the voltage change is all done.
313*4882a593Smuzhiyun 		 */
314*4882a593Smuzhiyun 		clk_en_a = mci_readl(host, CLKENA);
315*4882a593Smuzhiyun 		clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
316*4882a593Smuzhiyun 		mci_writel(host, CLKENA, clk_en_a);
317*4882a593Smuzhiyun 		mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
318*4882a593Smuzhiyun 			     SDMMC_CMD_PRV_DAT_WAIT, 0);
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (cmd->flags & MMC_RSP_PRESENT) {
322*4882a593Smuzhiyun 		/* We expect a response, so set this bit */
323*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_RESP_EXP;
324*4882a593Smuzhiyun 		if (cmd->flags & MMC_RSP_136)
325*4882a593Smuzhiyun 			cmdr |= SDMMC_CMD_RESP_LONG;
326*4882a593Smuzhiyun 	}
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (cmd->flags & MMC_RSP_CRC)
329*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_RESP_CRC;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (cmd->data) {
332*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_DAT_EXP;
333*4882a593Smuzhiyun 		if (cmd->data->flags & MMC_DATA_WRITE)
334*4882a593Smuzhiyun 			cmdr |= SDMMC_CMD_DAT_WR;
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
338*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	return cmdr;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
dw_mci_prep_stop_abort(struct dw_mci * host,struct mmc_command * cmd)343*4882a593Smuzhiyun static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct mmc_command *stop;
346*4882a593Smuzhiyun 	u32 cmdr;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	if (!cmd->data)
349*4882a593Smuzhiyun 		return 0;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	stop = &host->stop_abort;
352*4882a593Smuzhiyun 	cmdr = cmd->opcode;
353*4882a593Smuzhiyun 	memset(stop, 0, sizeof(struct mmc_command));
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	if (cmdr == MMC_READ_SINGLE_BLOCK ||
356*4882a593Smuzhiyun 	    cmdr == MMC_READ_MULTIPLE_BLOCK ||
357*4882a593Smuzhiyun 	    cmdr == MMC_WRITE_BLOCK ||
358*4882a593Smuzhiyun 	    cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
359*4882a593Smuzhiyun 	    cmdr == MMC_SEND_TUNING_BLOCK ||
360*4882a593Smuzhiyun 	    cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
361*4882a593Smuzhiyun 		stop->opcode = MMC_STOP_TRANSMISSION;
362*4882a593Smuzhiyun 		stop->arg = 0;
363*4882a593Smuzhiyun 		stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
364*4882a593Smuzhiyun 	} else if (cmdr == SD_IO_RW_EXTENDED) {
365*4882a593Smuzhiyun 		stop->opcode = SD_IO_RW_DIRECT;
366*4882a593Smuzhiyun 		stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
367*4882a593Smuzhiyun 			     ((cmd->arg >> 28) & 0x7);
368*4882a593Smuzhiyun 		stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
369*4882a593Smuzhiyun 	} else {
370*4882a593Smuzhiyun 		return 0;
371*4882a593Smuzhiyun 	}
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	cmdr = stop->opcode | SDMMC_CMD_STOP |
374*4882a593Smuzhiyun 		SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags))
377*4882a593Smuzhiyun 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	return cmdr;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
dw_mci_set_cto(struct dw_mci * host)382*4882a593Smuzhiyun static inline void dw_mci_set_cto(struct dw_mci *host)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	unsigned int cto_clks;
385*4882a593Smuzhiyun 	unsigned int cto_div;
386*4882a593Smuzhiyun 	unsigned int cto_ms;
387*4882a593Smuzhiyun 	unsigned long irqflags;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	cto_clks = mci_readl(host, TMOUT) & 0xff;
390*4882a593Smuzhiyun 	cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
391*4882a593Smuzhiyun 	if (cto_div == 0)
392*4882a593Smuzhiyun 		cto_div = 1;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
395*4882a593Smuzhiyun 				  host->bus_hz);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* add a bit spare time */
398*4882a593Smuzhiyun 	cto_ms += 10;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/*
401*4882a593Smuzhiyun 	 * The durations we're working with are fairly short so we have to be
402*4882a593Smuzhiyun 	 * extra careful about synchronization here.  Specifically in hardware a
403*4882a593Smuzhiyun 	 * command timeout is _at most_ 5.1 ms, so that means we expect an
404*4882a593Smuzhiyun 	 * interrupt (either command done or timeout) to come rather quickly
405*4882a593Smuzhiyun 	 * after the mci_writel.  ...but just in case we have a long interrupt
406*4882a593Smuzhiyun 	 * latency let's add a bit of paranoia.
407*4882a593Smuzhiyun 	 *
408*4882a593Smuzhiyun 	 * In general we'll assume that at least an interrupt will be asserted
409*4882a593Smuzhiyun 	 * in hardware by the time the cto_timer runs.  ...and if it hasn't
410*4882a593Smuzhiyun 	 * been asserted in hardware by that time then we'll assume it'll never
411*4882a593Smuzhiyun 	 * come.
412*4882a593Smuzhiyun 	 */
413*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
414*4882a593Smuzhiyun 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
415*4882a593Smuzhiyun 		mod_timer(&host->cto_timer,
416*4882a593Smuzhiyun 			jiffies + msecs_to_jiffies(cto_ms) + 1);
417*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
dw_mci_start_command(struct dw_mci * host,struct mmc_command * cmd,u32 cmd_flags)420*4882a593Smuzhiyun static void dw_mci_start_command(struct dw_mci *host,
421*4882a593Smuzhiyun 				 struct mmc_command *cmd, u32 cmd_flags)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	host->cmd = cmd;
424*4882a593Smuzhiyun 	dev_vdbg(host->dev,
425*4882a593Smuzhiyun 		 "start command: ARGR=0x%08x CMDR=0x%08x\n",
426*4882a593Smuzhiyun 		 cmd->arg, cmd_flags);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	mci_writel(host, CMDARG, cmd->arg);
429*4882a593Smuzhiyun 	wmb(); /* drain writebuffer */
430*4882a593Smuzhiyun 	dw_mci_wait_while_busy(host, cmd_flags);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* response expected command only */
435*4882a593Smuzhiyun 	if (cmd_flags & SDMMC_CMD_RESP_EXP)
436*4882a593Smuzhiyun 		dw_mci_set_cto(host);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
send_stop_abort(struct dw_mci * host,struct mmc_data * data)439*4882a593Smuzhiyun static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct mmc_command *stop = &host->stop_abort;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	dw_mci_start_command(host, stop, host->stop_cmdr);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /* DMA interface functions */
dw_mci_stop_dma(struct dw_mci * host)447*4882a593Smuzhiyun static void dw_mci_stop_dma(struct dw_mci *host)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	if (host->using_dma) {
450*4882a593Smuzhiyun 		host->dma_ops->stop(host);
451*4882a593Smuzhiyun 		host->dma_ops->cleanup(host);
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* Data transfer was stopped by the interrupt handler */
455*4882a593Smuzhiyun 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
dw_mci_dma_cleanup(struct dw_mci * host)458*4882a593Smuzhiyun static void dw_mci_dma_cleanup(struct dw_mci *host)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct mmc_data *data = host->data;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	if (data && data->host_cookie == COOKIE_MAPPED) {
463*4882a593Smuzhiyun 		dma_unmap_sg(host->dev,
464*4882a593Smuzhiyun 			     data->sg,
465*4882a593Smuzhiyun 			     data->sg_len,
466*4882a593Smuzhiyun 			     mmc_get_dma_dir(data));
467*4882a593Smuzhiyun 		data->host_cookie = COOKIE_UNMAPPED;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
dw_mci_idmac_reset(struct dw_mci * host)471*4882a593Smuzhiyun static void dw_mci_idmac_reset(struct dw_mci *host)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	u32 bmod = mci_readl(host, BMOD);
474*4882a593Smuzhiyun 	/* Software reset of DMA */
475*4882a593Smuzhiyun 	bmod |= SDMMC_IDMAC_SWRESET;
476*4882a593Smuzhiyun 	mci_writel(host, BMOD, bmod);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
dw_mci_idmac_stop_dma(struct dw_mci * host)479*4882a593Smuzhiyun static void dw_mci_idmac_stop_dma(struct dw_mci *host)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	u32 temp;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* Disable and reset the IDMAC interface */
484*4882a593Smuzhiyun 	temp = mci_readl(host, CTRL);
485*4882a593Smuzhiyun 	if (!host->is_rv1106_sd)
486*4882a593Smuzhiyun 		temp &= ~SDMMC_CTRL_USE_IDMAC;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	temp |= SDMMC_CTRL_DMA_RESET;
489*4882a593Smuzhiyun 	mci_writel(host, CTRL, temp);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* Stop the IDMAC running */
492*4882a593Smuzhiyun 	temp = mci_readl(host, BMOD);
493*4882a593Smuzhiyun 	if (host->is_rv1106_sd) {
494*4882a593Smuzhiyun 		temp |= SDMMC_IDMAC_SWRESET;
495*4882a593Smuzhiyun 	} else {
496*4882a593Smuzhiyun 		temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
497*4882a593Smuzhiyun 		temp |= SDMMC_IDMAC_SWRESET;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 	mci_writel(host, BMOD, temp);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun 
dw_mci_dmac_complete_dma(void * arg)502*4882a593Smuzhiyun static void dw_mci_dmac_complete_dma(void *arg)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	struct dw_mci *host = arg;
505*4882a593Smuzhiyun 	struct mmc_data *data = host->data;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	dev_vdbg(host->dev, "DMA complete\n");
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	if ((host->use_dma == TRANS_MODE_EDMAC) &&
510*4882a593Smuzhiyun 	    data && (data->flags & MMC_DATA_READ))
511*4882a593Smuzhiyun 		/* Invalidate cache after read */
512*4882a593Smuzhiyun 		dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc),
513*4882a593Smuzhiyun 				    data->sg,
514*4882a593Smuzhiyun 				    data->sg_len,
515*4882a593Smuzhiyun 				    DMA_FROM_DEVICE);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	host->dma_ops->cleanup(host);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/*
520*4882a593Smuzhiyun 	 * If the card was removed, data will be NULL. No point in trying to
521*4882a593Smuzhiyun 	 * send the stop command or waiting for NBUSY in this case.
522*4882a593Smuzhiyun 	 */
523*4882a593Smuzhiyun 	if (data) {
524*4882a593Smuzhiyun 		set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
525*4882a593Smuzhiyun 		tasklet_schedule(&host->tasklet);
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (host->need_xfer_timer &&
529*4882a593Smuzhiyun 	    host->dir_status == DW_MCI_RECV_STATUS)
530*4882a593Smuzhiyun 		del_timer(&host->xfer_timer);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
dw_mci_idmac_init(struct dw_mci * host)533*4882a593Smuzhiyun static int dw_mci_idmac_init(struct dw_mci *host)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	int i;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	if (host->dma_64bit_address == 1) {
538*4882a593Smuzhiyun 		struct idmac_desc_64addr *p;
539*4882a593Smuzhiyun 		/* Number of descriptors in the ring buffer */
540*4882a593Smuzhiyun 		host->ring_size =
541*4882a593Smuzhiyun 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		/* Forward link the descriptor list */
544*4882a593Smuzhiyun 		for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
545*4882a593Smuzhiyun 								i++, p++) {
546*4882a593Smuzhiyun 			p->des6 = (host->sg_dma +
547*4882a593Smuzhiyun 					(sizeof(struct idmac_desc_64addr) *
548*4882a593Smuzhiyun 							(i + 1))) & 0xffffffff;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 			p->des7 = (u64)(host->sg_dma +
551*4882a593Smuzhiyun 					(sizeof(struct idmac_desc_64addr) *
552*4882a593Smuzhiyun 							(i + 1))) >> 32;
553*4882a593Smuzhiyun 			/* Initialize reserved and buffer size fields to "0" */
554*4882a593Smuzhiyun 			p->des0 = 0;
555*4882a593Smuzhiyun 			p->des1 = 0;
556*4882a593Smuzhiyun 			p->des2 = 0;
557*4882a593Smuzhiyun 			p->des3 = 0;
558*4882a593Smuzhiyun 		}
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		/* Set the last descriptor as the end-of-ring descriptor */
561*4882a593Smuzhiyun 		p->des6 = host->sg_dma & 0xffffffff;
562*4882a593Smuzhiyun 		p->des7 = (u64)host->sg_dma >> 32;
563*4882a593Smuzhiyun 		p->des0 = IDMAC_DES0_ER;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	} else {
566*4882a593Smuzhiyun 		struct idmac_desc *p;
567*4882a593Smuzhiyun 		/* Number of descriptors in the ring buffer */
568*4882a593Smuzhiyun 		host->ring_size =
569*4882a593Smuzhiyun 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 		/* Forward link the descriptor list */
572*4882a593Smuzhiyun 		for (i = 0, p = host->sg_cpu;
573*4882a593Smuzhiyun 		     i < host->ring_size - 1;
574*4882a593Smuzhiyun 		     i++, p++) {
575*4882a593Smuzhiyun 			p->des3 = cpu_to_le32(host->sg_dma +
576*4882a593Smuzhiyun 					(sizeof(struct idmac_desc) * (i + 1)));
577*4882a593Smuzhiyun 			p->des0 = 0;
578*4882a593Smuzhiyun 			p->des1 = 0;
579*4882a593Smuzhiyun 		}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 		/* Set the last descriptor as the end-of-ring descriptor */
582*4882a593Smuzhiyun 		p->des3 = cpu_to_le32(host->sg_dma);
583*4882a593Smuzhiyun 		p->des0 = cpu_to_le32(IDMAC_DES0_ER);
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	dw_mci_idmac_reset(host);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (host->dma_64bit_address == 1) {
589*4882a593Smuzhiyun 		/* Mask out interrupts - get Tx & Rx complete only */
590*4882a593Smuzhiyun 		mci_writel(host, IDSTS64, IDMAC_INT_CLR);
591*4882a593Smuzhiyun 		mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
592*4882a593Smuzhiyun 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 		/* Set the descriptor base address */
595*4882a593Smuzhiyun 		mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
596*4882a593Smuzhiyun 		mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	} else {
599*4882a593Smuzhiyun 		/* Mask out interrupts - get Tx & Rx complete only */
600*4882a593Smuzhiyun 		mci_writel(host, IDSTS, IDMAC_INT_CLR);
601*4882a593Smuzhiyun 		mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
602*4882a593Smuzhiyun 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 		/* Set the descriptor base address */
605*4882a593Smuzhiyun 		mci_writel(host, DBADDR, host->sg_dma);
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return 0;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
dw_mci_prepare_desc64(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)611*4882a593Smuzhiyun static inline int dw_mci_prepare_desc64(struct dw_mci *host,
612*4882a593Smuzhiyun 					 struct mmc_data *data,
613*4882a593Smuzhiyun 					 unsigned int sg_len)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun 	unsigned int desc_len;
616*4882a593Smuzhiyun 	struct idmac_desc_64addr *desc_first, *desc_last, *desc;
617*4882a593Smuzhiyun 	u32 val;
618*4882a593Smuzhiyun 	int i;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	desc_first = desc_last = desc = host->sg_cpu;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	for (i = 0; i < sg_len; i++) {
623*4882a593Smuzhiyun 		unsigned int length = sg_dma_len(&data->sg[i]);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 		u64 mem_addr = sg_dma_address(&data->sg[i]);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 		for ( ; length ; desc++) {
628*4882a593Smuzhiyun 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
629*4882a593Smuzhiyun 				   length : DW_MCI_DESC_DATA_LENGTH;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 			length -= desc_len;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 			/*
634*4882a593Smuzhiyun 			 * Wait for the former clear OWN bit operation
635*4882a593Smuzhiyun 			 * of IDMAC to make sure that this descriptor
636*4882a593Smuzhiyun 			 * isn't still owned by IDMAC as IDMAC's write
637*4882a593Smuzhiyun 			 * ops and CPU's read ops are asynchronous.
638*4882a593Smuzhiyun 			 */
639*4882a593Smuzhiyun 			if (readl_poll_timeout_atomic(&desc->des0, val,
640*4882a593Smuzhiyun 						!(val & IDMAC_DES0_OWN),
641*4882a593Smuzhiyun 						10, 100 * USEC_PER_MSEC))
642*4882a593Smuzhiyun 				goto err_own_bit;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 			/*
645*4882a593Smuzhiyun 			 * Set the OWN bit and disable interrupts
646*4882a593Smuzhiyun 			 * for this descriptor
647*4882a593Smuzhiyun 			 */
648*4882a593Smuzhiyun 			desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
649*4882a593Smuzhiyun 						IDMAC_DES0_CH;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 			/* Buffer length */
652*4882a593Smuzhiyun 			IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 			/* Physical address to DMA to/from */
655*4882a593Smuzhiyun 			desc->des4 = mem_addr & 0xffffffff;
656*4882a593Smuzhiyun 			desc->des5 = mem_addr >> 32;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 			/* Update physical address for the next desc */
659*4882a593Smuzhiyun 			mem_addr += desc_len;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 			/* Save pointer to the last descriptor */
662*4882a593Smuzhiyun 			desc_last = desc;
663*4882a593Smuzhiyun 		}
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	/* Set first descriptor */
667*4882a593Smuzhiyun 	desc_first->des0 |= IDMAC_DES0_FD;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Set last descriptor */
670*4882a593Smuzhiyun 	desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
671*4882a593Smuzhiyun 	desc_last->des0 |= IDMAC_DES0_LD;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	return 0;
674*4882a593Smuzhiyun err_own_bit:
675*4882a593Smuzhiyun 	/* restore the descriptor chain as it's polluted */
676*4882a593Smuzhiyun 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
677*4882a593Smuzhiyun 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
678*4882a593Smuzhiyun 	dw_mci_idmac_init(host);
679*4882a593Smuzhiyun 	return -EINVAL;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 
dw_mci_prepare_desc32(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)683*4882a593Smuzhiyun static inline int dw_mci_prepare_desc32(struct dw_mci *host,
684*4882a593Smuzhiyun 					 struct mmc_data *data,
685*4882a593Smuzhiyun 					 unsigned int sg_len)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	unsigned int desc_len;
688*4882a593Smuzhiyun 	struct idmac_desc *desc_first, *desc_last, *desc;
689*4882a593Smuzhiyun 	u32 val;
690*4882a593Smuzhiyun 	int i;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	desc_first = desc_last = desc = host->sg_cpu;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	for (i = 0; i < sg_len; i++) {
695*4882a593Smuzhiyun 		unsigned int length = sg_dma_len(&data->sg[i]);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 		u32 mem_addr = sg_dma_address(&data->sg[i]);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 		for ( ; length ; desc++) {
700*4882a593Smuzhiyun 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
701*4882a593Smuzhiyun 				   length : DW_MCI_DESC_DATA_LENGTH;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 			length -= desc_len;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 			/*
706*4882a593Smuzhiyun 			 * Wait for the former clear OWN bit operation
707*4882a593Smuzhiyun 			 * of IDMAC to make sure that this descriptor
708*4882a593Smuzhiyun 			 * isn't still owned by IDMAC as IDMAC's write
709*4882a593Smuzhiyun 			 * ops and CPU's read ops are asynchronous.
710*4882a593Smuzhiyun 			 */
711*4882a593Smuzhiyun 			if (readl_poll_timeout_atomic(&desc->des0, val,
712*4882a593Smuzhiyun 						      IDMAC_OWN_CLR64(val),
713*4882a593Smuzhiyun 						      10,
714*4882a593Smuzhiyun 						      100 * USEC_PER_MSEC))
715*4882a593Smuzhiyun 				goto err_own_bit;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 			/*
718*4882a593Smuzhiyun 			 * Set the OWN bit and disable interrupts
719*4882a593Smuzhiyun 			 * for this descriptor
720*4882a593Smuzhiyun 			 */
721*4882a593Smuzhiyun 			desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
722*4882a593Smuzhiyun 						 IDMAC_DES0_DIC |
723*4882a593Smuzhiyun 						 IDMAC_DES0_CH);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 			/* Buffer length */
726*4882a593Smuzhiyun 			IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 			/* Physical address to DMA to/from */
729*4882a593Smuzhiyun 			desc->des2 = cpu_to_le32(mem_addr);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 			/* Update physical address for the next desc */
732*4882a593Smuzhiyun 			mem_addr += desc_len;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 			/* Save pointer to the last descriptor */
735*4882a593Smuzhiyun 			desc_last = desc;
736*4882a593Smuzhiyun 		}
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE)) {
740*4882a593Smuzhiyun 		desc->des0 = desc_last->des0;
741*4882a593Smuzhiyun 		desc->des2 = desc_last->des2;
742*4882a593Smuzhiyun 		desc->des1 = 0x8; /* Random dirty data for last one desc */
743*4882a593Smuzhiyun 		desc_last = desc;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	/* Set first descriptor */
747*4882a593Smuzhiyun 	desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/* Set last descriptor */
750*4882a593Smuzhiyun 	desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
751*4882a593Smuzhiyun 				       IDMAC_DES0_DIC));
752*4882a593Smuzhiyun 	desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	return 0;
755*4882a593Smuzhiyun err_own_bit:
756*4882a593Smuzhiyun 	/* restore the descriptor chain as it's polluted */
757*4882a593Smuzhiyun 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
758*4882a593Smuzhiyun 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
759*4882a593Smuzhiyun 	dw_mci_idmac_init(host);
760*4882a593Smuzhiyun 	return -EINVAL;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
dw_mci_idmac_start_dma(struct dw_mci * host,unsigned int sg_len)763*4882a593Smuzhiyun static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	u32 temp;
766*4882a593Smuzhiyun 	int ret;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	if (host->dma_64bit_address == 1)
769*4882a593Smuzhiyun 		ret = dw_mci_prepare_desc64(host, host->data, sg_len);
770*4882a593Smuzhiyun 	else
771*4882a593Smuzhiyun 		ret = dw_mci_prepare_desc32(host, host->data, sg_len);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (ret)
774*4882a593Smuzhiyun 		goto out;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	/* drain writebuffer */
777*4882a593Smuzhiyun 	wmb();
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	/* Make sure to reset DMA in case we did PIO before this */
780*4882a593Smuzhiyun 	dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
781*4882a593Smuzhiyun 	dw_mci_idmac_reset(host);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* Select IDMAC interface */
784*4882a593Smuzhiyun 	temp = mci_readl(host, CTRL);
785*4882a593Smuzhiyun 	temp |= SDMMC_CTRL_USE_IDMAC;
786*4882a593Smuzhiyun 	mci_writel(host, CTRL, temp);
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	/* drain writebuffer */
789*4882a593Smuzhiyun 	wmb();
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	/* Enable the IDMAC */
792*4882a593Smuzhiyun 	temp = mci_readl(host, BMOD);
793*4882a593Smuzhiyun 	temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
794*4882a593Smuzhiyun 	mci_writel(host, BMOD, temp);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	/* Start it running */
797*4882a593Smuzhiyun 	mci_writel(host, PLDMND, 1);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun out:
800*4882a593Smuzhiyun 	return ret;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
804*4882a593Smuzhiyun 	.init = dw_mci_idmac_init,
805*4882a593Smuzhiyun 	.start = dw_mci_idmac_start_dma,
806*4882a593Smuzhiyun 	.stop = dw_mci_idmac_stop_dma,
807*4882a593Smuzhiyun 	.complete = dw_mci_dmac_complete_dma,
808*4882a593Smuzhiyun 	.cleanup = dw_mci_dma_cleanup,
809*4882a593Smuzhiyun };
810*4882a593Smuzhiyun 
dw_mci_edmac_stop_dma(struct dw_mci * host)811*4882a593Smuzhiyun static void dw_mci_edmac_stop_dma(struct dw_mci *host)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun 	dmaengine_terminate_async(host->dms->ch);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun 
dw_mci_edmac_start_dma(struct dw_mci * host,unsigned int sg_len)816*4882a593Smuzhiyun static int dw_mci_edmac_start_dma(struct dw_mci *host,
817*4882a593Smuzhiyun 					    unsigned int sg_len)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	struct dma_slave_config cfg;
820*4882a593Smuzhiyun 	struct dma_async_tx_descriptor *desc = NULL;
821*4882a593Smuzhiyun 	struct scatterlist *sgl = host->data->sg;
822*4882a593Smuzhiyun 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
823*4882a593Smuzhiyun 	u32 sg_elems = host->data->sg_len;
824*4882a593Smuzhiyun 	u32 fifoth_val;
825*4882a593Smuzhiyun 	u32 fifo_offset = host->fifo_reg - host->regs;
826*4882a593Smuzhiyun 	int ret = 0;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	/* Set external dma config: burst size, burst width */
829*4882a593Smuzhiyun 	memset(&cfg, 0, sizeof(cfg));
830*4882a593Smuzhiyun 	cfg.dst_addr = host->phy_regs + fifo_offset;
831*4882a593Smuzhiyun 	cfg.src_addr = cfg.dst_addr;
832*4882a593Smuzhiyun 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
833*4882a593Smuzhiyun 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* Match burst msize with external dma config */
836*4882a593Smuzhiyun 	fifoth_val = mci_readl(host, FIFOTH);
837*4882a593Smuzhiyun 	cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
838*4882a593Smuzhiyun 	cfg.src_maxburst = cfg.dst_maxburst;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	if (host->data->flags & MMC_DATA_WRITE)
841*4882a593Smuzhiyun 		cfg.direction = DMA_MEM_TO_DEV;
842*4882a593Smuzhiyun 	else
843*4882a593Smuzhiyun 		cfg.direction = DMA_DEV_TO_MEM;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	ret = dmaengine_slave_config(host->dms->ch, &cfg);
846*4882a593Smuzhiyun 	if (ret) {
847*4882a593Smuzhiyun 		dev_err(host->dev, "Failed to config edmac.\n");
848*4882a593Smuzhiyun 		return -EBUSY;
849*4882a593Smuzhiyun 	}
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
852*4882a593Smuzhiyun 				       sg_len, cfg.direction,
853*4882a593Smuzhiyun 				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
854*4882a593Smuzhiyun 	if (!desc) {
855*4882a593Smuzhiyun 		dev_err(host->dev, "Can't prepare slave sg.\n");
856*4882a593Smuzhiyun 		return -EBUSY;
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	/* Set dw_mci_dmac_complete_dma as callback */
860*4882a593Smuzhiyun 	desc->callback = dw_mci_dmac_complete_dma;
861*4882a593Smuzhiyun 	desc->callback_param = (void *)host;
862*4882a593Smuzhiyun 	dmaengine_submit(desc);
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	/* Flush cache before write */
865*4882a593Smuzhiyun 	if (host->data->flags & MMC_DATA_WRITE)
866*4882a593Smuzhiyun 		dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl,
867*4882a593Smuzhiyun 				       sg_elems, DMA_TO_DEVICE);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	dma_async_issue_pending(host->dms->ch);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
dw_mci_edmac_init(struct dw_mci * host)874*4882a593Smuzhiyun static int dw_mci_edmac_init(struct dw_mci *host)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	/* Request external dma channel */
877*4882a593Smuzhiyun 	host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
878*4882a593Smuzhiyun 	if (!host->dms)
879*4882a593Smuzhiyun 		return -ENOMEM;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	host->dms->ch = dma_request_chan(host->dev, "rx-tx");
882*4882a593Smuzhiyun 	if (IS_ERR(host->dms->ch)) {
883*4882a593Smuzhiyun 		int ret = PTR_ERR(host->dms->ch);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 		dev_err(host->dev, "Failed to get external DMA channel.\n");
886*4882a593Smuzhiyun 		kfree(host->dms);
887*4882a593Smuzhiyun 		host->dms = NULL;
888*4882a593Smuzhiyun 		return ret;
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	return 0;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun 
dw_mci_edmac_exit(struct dw_mci * host)894*4882a593Smuzhiyun static void dw_mci_edmac_exit(struct dw_mci *host)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	if (host->dms) {
897*4882a593Smuzhiyun 		if (host->dms->ch) {
898*4882a593Smuzhiyun 			dma_release_channel(host->dms->ch);
899*4882a593Smuzhiyun 			host->dms->ch = NULL;
900*4882a593Smuzhiyun 		}
901*4882a593Smuzhiyun 		kfree(host->dms);
902*4882a593Smuzhiyun 		host->dms = NULL;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
907*4882a593Smuzhiyun 	.init = dw_mci_edmac_init,
908*4882a593Smuzhiyun 	.exit = dw_mci_edmac_exit,
909*4882a593Smuzhiyun 	.start = dw_mci_edmac_start_dma,
910*4882a593Smuzhiyun 	.stop = dw_mci_edmac_stop_dma,
911*4882a593Smuzhiyun 	.complete = dw_mci_dmac_complete_dma,
912*4882a593Smuzhiyun 	.cleanup = dw_mci_dma_cleanup,
913*4882a593Smuzhiyun };
914*4882a593Smuzhiyun 
dw_mci_pre_dma_transfer(struct dw_mci * host,struct mmc_data * data,int cookie)915*4882a593Smuzhiyun static int dw_mci_pre_dma_transfer(struct dw_mci *host,
916*4882a593Smuzhiyun 				   struct mmc_data *data,
917*4882a593Smuzhiyun 				   int cookie)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct scatterlist *sg;
920*4882a593Smuzhiyun 	unsigned int i, sg_len;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	if (data->host_cookie == COOKIE_PRE_MAPPED)
923*4882a593Smuzhiyun 		return data->sg_len;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	/*
926*4882a593Smuzhiyun 	 * We don't do DMA on "complex" transfers, i.e. with
927*4882a593Smuzhiyun 	 * non-word-aligned buffers or lengths. Also, we don't bother
928*4882a593Smuzhiyun 	 * with all the DMA setup overhead for short transfers.
929*4882a593Smuzhiyun 	 */
930*4882a593Smuzhiyun 	if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD && !host->is_rv1106_sd)
931*4882a593Smuzhiyun 		return -EINVAL;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	if (data->blksz & 3)
934*4882a593Smuzhiyun 		return -EINVAL;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	for_each_sg(data->sg, sg, data->sg_len, i) {
937*4882a593Smuzhiyun 		if (sg->offset & 3 || sg->length & 3)
938*4882a593Smuzhiyun 			return -EINVAL;
939*4882a593Smuzhiyun 	}
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	sg_len = dma_map_sg(host->dev,
942*4882a593Smuzhiyun 			    data->sg,
943*4882a593Smuzhiyun 			    data->sg_len,
944*4882a593Smuzhiyun 			    mmc_get_dma_dir(data));
945*4882a593Smuzhiyun 	if (sg_len == 0)
946*4882a593Smuzhiyun 		return -EINVAL;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	data->host_cookie = cookie;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	return sg_len;
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun 
dw_mci_pre_req(struct mmc_host * mmc,struct mmc_request * mrq)953*4882a593Smuzhiyun static void dw_mci_pre_req(struct mmc_host *mmc,
954*4882a593Smuzhiyun 			   struct mmc_request *mrq)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
957*4882a593Smuzhiyun 	struct mmc_data *data = mrq->data;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	if (!slot->host->use_dma || !data)
960*4882a593Smuzhiyun 		return;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	/* This data might be unmapped at this time */
963*4882a593Smuzhiyun 	data->host_cookie = COOKIE_UNMAPPED;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
966*4882a593Smuzhiyun 				COOKIE_PRE_MAPPED) < 0)
967*4882a593Smuzhiyun 		data->host_cookie = COOKIE_UNMAPPED;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
dw_mci_post_req(struct mmc_host * mmc,struct mmc_request * mrq,int err)970*4882a593Smuzhiyun static void dw_mci_post_req(struct mmc_host *mmc,
971*4882a593Smuzhiyun 			    struct mmc_request *mrq,
972*4882a593Smuzhiyun 			    int err)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
975*4882a593Smuzhiyun 	struct mmc_data *data = mrq->data;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	if (!slot->host->use_dma || !data)
978*4882a593Smuzhiyun 		return;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	if (data->host_cookie != COOKIE_UNMAPPED)
981*4882a593Smuzhiyun 		dma_unmap_sg(slot->host->dev,
982*4882a593Smuzhiyun 			     data->sg,
983*4882a593Smuzhiyun 			     data->sg_len,
984*4882a593Smuzhiyun 			     mmc_get_dma_dir(data));
985*4882a593Smuzhiyun 	data->host_cookie = COOKIE_UNMAPPED;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
dw_mci_get_cd(struct mmc_host * mmc)988*4882a593Smuzhiyun static int dw_mci_get_cd(struct mmc_host *mmc)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun 	int present;
991*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
992*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
993*4882a593Smuzhiyun 	int gpio_cd = mmc_gpio_get_cd(mmc);
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	/* Use platform get_cd function, else try onboard card detect */
996*4882a593Smuzhiyun 	if (((mmc->caps & MMC_CAP_NEEDS_POLL)
997*4882a593Smuzhiyun 				|| !mmc_card_is_removable(mmc))) {
998*4882a593Smuzhiyun 		present = 1;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 		if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1001*4882a593Smuzhiyun 			if (mmc->caps & MMC_CAP_NEEDS_POLL) {
1002*4882a593Smuzhiyun 				dev_info(&mmc->class_dev,
1003*4882a593Smuzhiyun 					"card is polling.\n");
1004*4882a593Smuzhiyun 			} else {
1005*4882a593Smuzhiyun 				dev_info(&mmc->class_dev,
1006*4882a593Smuzhiyun 					"card is non-removable.\n");
1007*4882a593Smuzhiyun 			}
1008*4882a593Smuzhiyun 			set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1009*4882a593Smuzhiyun 		}
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 		return present;
1012*4882a593Smuzhiyun 	} else if (gpio_cd >= 0)
1013*4882a593Smuzhiyun 		present = gpio_cd;
1014*4882a593Smuzhiyun 	else
1015*4882a593Smuzhiyun 		present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1016*4882a593Smuzhiyun 			== 0 ? 1 : 0;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	spin_lock_bh(&host->lock);
1019*4882a593Smuzhiyun 	if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
1020*4882a593Smuzhiyun 		dev_dbg(&mmc->class_dev, "card is present\n");
1021*4882a593Smuzhiyun 	else if (!present &&
1022*4882a593Smuzhiyun 			!test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
1023*4882a593Smuzhiyun 		dev_dbg(&mmc->class_dev, "card is not present\n");
1024*4882a593Smuzhiyun 	spin_unlock_bh(&host->lock);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	return present;
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun 
dw_mci_adjust_fifoth(struct dw_mci * host,struct mmc_data * data)1029*4882a593Smuzhiyun static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun 	unsigned int blksz = data->blksz;
1032*4882a593Smuzhiyun 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
1033*4882a593Smuzhiyun 	u32 fifo_width = 1 << host->data_shift;
1034*4882a593Smuzhiyun 	u32 blksz_depth = blksz / fifo_width, fifoth_val;
1035*4882a593Smuzhiyun 	u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
1036*4882a593Smuzhiyun 	int idx = ARRAY_SIZE(mszs) - 1;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	/* pio should ship this scenario */
1039*4882a593Smuzhiyun 	if (!host->use_dma)
1040*4882a593Smuzhiyun 		return;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	tx_wmark = (host->fifo_depth) / 2;
1043*4882a593Smuzhiyun 	tx_wmark_invers = host->fifo_depth - tx_wmark;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	/*
1046*4882a593Smuzhiyun 	 * MSIZE is '1',
1047*4882a593Smuzhiyun 	 * if blksz is not a multiple of the FIFO width
1048*4882a593Smuzhiyun 	 */
1049*4882a593Smuzhiyun 	if (blksz % fifo_width)
1050*4882a593Smuzhiyun 		goto done;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	do {
1053*4882a593Smuzhiyun 		if (!((blksz_depth % mszs[idx]) ||
1054*4882a593Smuzhiyun 		     (tx_wmark_invers % mszs[idx]))) {
1055*4882a593Smuzhiyun 			msize = idx;
1056*4882a593Smuzhiyun 			rx_wmark = mszs[idx] - 1;
1057*4882a593Smuzhiyun 			break;
1058*4882a593Smuzhiyun 		}
1059*4882a593Smuzhiyun 	} while (--idx > 0);
1060*4882a593Smuzhiyun 	/*
1061*4882a593Smuzhiyun 	 * If idx is '0', it won't be tried
1062*4882a593Smuzhiyun 	 * Thus, initial values are uesed
1063*4882a593Smuzhiyun 	 */
1064*4882a593Smuzhiyun done:
1065*4882a593Smuzhiyun 	fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
1066*4882a593Smuzhiyun 	mci_writel(host, FIFOTH, fifoth_val);
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun 
dw_mci_ctrl_thld(struct dw_mci * host,struct mmc_data * data)1069*4882a593Smuzhiyun static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
1070*4882a593Smuzhiyun {
1071*4882a593Smuzhiyun 	unsigned int blksz = data->blksz;
1072*4882a593Smuzhiyun 	u32 blksz_depth, fifo_depth;
1073*4882a593Smuzhiyun 	u16 thld_size;
1074*4882a593Smuzhiyun 	u8 enable;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	/*
1077*4882a593Smuzhiyun 	 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
1078*4882a593Smuzhiyun 	 * in the FIFO region, so we really shouldn't access it).
1079*4882a593Smuzhiyun 	 */
1080*4882a593Smuzhiyun 	if (host->verid < DW_MMC_240A ||
1081*4882a593Smuzhiyun 		(host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
1082*4882a593Smuzhiyun 		return;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	/*
1085*4882a593Smuzhiyun 	 * Card write Threshold is introduced since 2.80a
1086*4882a593Smuzhiyun 	 * It's used when HS400 mode is enabled.
1087*4882a593Smuzhiyun 	 */
1088*4882a593Smuzhiyun 	if (data->flags & MMC_DATA_WRITE &&
1089*4882a593Smuzhiyun 		host->timing != MMC_TIMING_MMC_HS400)
1090*4882a593Smuzhiyun 		goto disable;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	if (data->flags & MMC_DATA_WRITE)
1093*4882a593Smuzhiyun 		enable = SDMMC_CARD_WR_THR_EN;
1094*4882a593Smuzhiyun 	else
1095*4882a593Smuzhiyun 		enable = SDMMC_CARD_RD_THR_EN;
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	if (host->timing != MMC_TIMING_MMC_HS200 &&
1098*4882a593Smuzhiyun 	    host->timing != MMC_TIMING_UHS_SDR104 &&
1099*4882a593Smuzhiyun 	    host->timing != MMC_TIMING_MMC_HS400)
1100*4882a593Smuzhiyun 		goto disable;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	blksz_depth = blksz / (1 << host->data_shift);
1103*4882a593Smuzhiyun 	fifo_depth = host->fifo_depth;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	if (blksz_depth > fifo_depth)
1106*4882a593Smuzhiyun 		goto disable;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	/*
1109*4882a593Smuzhiyun 	 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1110*4882a593Smuzhiyun 	 * If (blksz_depth) <  (fifo_depth >> 1), should be thld_size = blksz
1111*4882a593Smuzhiyun 	 * Currently just choose blksz.
1112*4882a593Smuzhiyun 	 */
1113*4882a593Smuzhiyun 	thld_size = blksz;
1114*4882a593Smuzhiyun 	mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
1115*4882a593Smuzhiyun 	return;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun disable:
1118*4882a593Smuzhiyun 	mci_writel(host, CDTHRCTL, 0);
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun 
dw_mci_submit_data_dma(struct dw_mci * host,struct mmc_data * data)1121*4882a593Smuzhiyun static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	unsigned long irqflags;
1124*4882a593Smuzhiyun 	int sg_len;
1125*4882a593Smuzhiyun 	u32 temp;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	host->using_dma = 0;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	/* If we don't have a channel, we can't do DMA */
1130*4882a593Smuzhiyun 	if (!host->use_dma)
1131*4882a593Smuzhiyun 		return -ENODEV;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
1134*4882a593Smuzhiyun 	if (sg_len < 0) {
1135*4882a593Smuzhiyun 		host->dma_ops->stop(host);
1136*4882a593Smuzhiyun 		return sg_len;
1137*4882a593Smuzhiyun 	}
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	host->using_dma = 1;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	if (host->use_dma == TRANS_MODE_IDMAC)
1142*4882a593Smuzhiyun 		dev_vdbg(host->dev,
1143*4882a593Smuzhiyun 			 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1144*4882a593Smuzhiyun 			 (unsigned long)host->sg_cpu,
1145*4882a593Smuzhiyun 			 (unsigned long)host->sg_dma,
1146*4882a593Smuzhiyun 			 sg_len);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	/*
1149*4882a593Smuzhiyun 	 * Decide the MSIZE and RX/TX Watermark.
1150*4882a593Smuzhiyun 	 * If current block size is same with previous size,
1151*4882a593Smuzhiyun 	 * no need to update fifoth.
1152*4882a593Smuzhiyun 	 */
1153*4882a593Smuzhiyun 	if (host->prev_blksz != data->blksz)
1154*4882a593Smuzhiyun 		dw_mci_adjust_fifoth(host, data);
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	/* Enable the DMA interface */
1157*4882a593Smuzhiyun 	temp = mci_readl(host, CTRL);
1158*4882a593Smuzhiyun 	temp |= SDMMC_CTRL_DMA_ENABLE;
1159*4882a593Smuzhiyun 	mci_writel(host, CTRL, temp);
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	/* Disable RX/TX IRQs, let DMA handle it */
1162*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
1163*4882a593Smuzhiyun 	temp = mci_readl(host, INTMASK);
1164*4882a593Smuzhiyun 	temp  &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1165*4882a593Smuzhiyun 	mci_writel(host, INTMASK, temp);
1166*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	if (host->dma_ops->start(host, sg_len)) {
1169*4882a593Smuzhiyun 		host->dma_ops->stop(host);
1170*4882a593Smuzhiyun 		/* We can't do DMA, try PIO for this one */
1171*4882a593Smuzhiyun 		dev_dbg(host->dev,
1172*4882a593Smuzhiyun 			"%s: fall back to PIO mode for current transfer\n",
1173*4882a593Smuzhiyun 			__func__);
1174*4882a593Smuzhiyun 		return -ENODEV;
1175*4882a593Smuzhiyun 	}
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	return 0;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun 
dw_mci_submit_data(struct dw_mci * host,struct mmc_data * data)1180*4882a593Smuzhiyun static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun 	unsigned long irqflags;
1183*4882a593Smuzhiyun 	int flags = SG_MITER_ATOMIC;
1184*4882a593Smuzhiyun 	u32 temp;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	data->error = -EINPROGRESS;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	WARN_ON(host->data);
1189*4882a593Smuzhiyun 	host->sg = NULL;
1190*4882a593Smuzhiyun 	host->data = data;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	if (data->flags & MMC_DATA_READ)
1193*4882a593Smuzhiyun 		host->dir_status = DW_MCI_RECV_STATUS;
1194*4882a593Smuzhiyun 	else
1195*4882a593Smuzhiyun 		host->dir_status = DW_MCI_SEND_STATUS;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	dw_mci_ctrl_thld(host, data);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	if (dw_mci_submit_data_dma(host, data)) {
1200*4882a593Smuzhiyun 		if (host->data->flags & MMC_DATA_READ)
1201*4882a593Smuzhiyun 			flags |= SG_MITER_TO_SG;
1202*4882a593Smuzhiyun 		else
1203*4882a593Smuzhiyun 			flags |= SG_MITER_FROM_SG;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 		sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1206*4882a593Smuzhiyun 		host->sg = data->sg;
1207*4882a593Smuzhiyun 		host->part_buf_start = 0;
1208*4882a593Smuzhiyun 		host->part_buf_count = 0;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 		spin_lock_irqsave(&host->irq_lock, irqflags);
1213*4882a593Smuzhiyun 		temp = mci_readl(host, INTMASK);
1214*4882a593Smuzhiyun 		temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1215*4882a593Smuzhiyun 		mci_writel(host, INTMASK, temp);
1216*4882a593Smuzhiyun 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 		temp = mci_readl(host, CTRL);
1219*4882a593Smuzhiyun 		temp &= ~SDMMC_CTRL_DMA_ENABLE;
1220*4882a593Smuzhiyun 		mci_writel(host, CTRL, temp);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		/*
1223*4882a593Smuzhiyun 		 * Use the initial fifoth_val for PIO mode. If wm_algined
1224*4882a593Smuzhiyun 		 * is set, we set watermark same as data size.
1225*4882a593Smuzhiyun 		 * If next issued data may be transfered by DMA mode,
1226*4882a593Smuzhiyun 		 * prev_blksz should be invalidated.
1227*4882a593Smuzhiyun 		 */
1228*4882a593Smuzhiyun 		if (host->wm_aligned)
1229*4882a593Smuzhiyun 			dw_mci_adjust_fifoth(host, data);
1230*4882a593Smuzhiyun 		else
1231*4882a593Smuzhiyun 			mci_writel(host, FIFOTH, host->fifoth_val);
1232*4882a593Smuzhiyun 		host->prev_blksz = 0;
1233*4882a593Smuzhiyun 	} else {
1234*4882a593Smuzhiyun 		/*
1235*4882a593Smuzhiyun 		 * Keep the current block size.
1236*4882a593Smuzhiyun 		 * It will be used to decide whether to update
1237*4882a593Smuzhiyun 		 * fifoth register next time.
1238*4882a593Smuzhiyun 		 */
1239*4882a593Smuzhiyun 		host->prev_blksz = data->blksz;
1240*4882a593Smuzhiyun 	}
1241*4882a593Smuzhiyun }
1242*4882a593Smuzhiyun 
dw_mci_setup_bus(struct dw_mci_slot * slot,bool force_clkinit)1243*4882a593Smuzhiyun static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
1244*4882a593Smuzhiyun {
1245*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1246*4882a593Smuzhiyun 	unsigned int clock = slot->clock;
1247*4882a593Smuzhiyun 	u32 div;
1248*4882a593Smuzhiyun 	u32 clk_en_a;
1249*4882a593Smuzhiyun 	u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* We must continue to set bit 28 in CMD until the change is complete */
1252*4882a593Smuzhiyun 	if (host->state == STATE_WAITING_CMD11_DONE)
1253*4882a593Smuzhiyun 		sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	slot->mmc->actual_clock = 0;
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	if (!clock) {
1258*4882a593Smuzhiyun 		mci_writel(host, CLKENA, 0);
1259*4882a593Smuzhiyun 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1260*4882a593Smuzhiyun 	} else if (clock != host->current_speed || force_clkinit) {
1261*4882a593Smuzhiyun 		div = host->bus_hz / clock;
1262*4882a593Smuzhiyun 		if (host->bus_hz % clock && host->bus_hz > clock)
1263*4882a593Smuzhiyun 			/*
1264*4882a593Smuzhiyun 			 * move the + 1 after the divide to prevent
1265*4882a593Smuzhiyun 			 * over-clocking the card.
1266*4882a593Smuzhiyun 			 */
1267*4882a593Smuzhiyun 			div += 1;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 		div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 		if ((clock != slot->__clk_old &&
1272*4882a593Smuzhiyun 			!test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1273*4882a593Smuzhiyun 			force_clkinit) {
1274*4882a593Smuzhiyun 			/* Silent the verbose log if calling from PM context */
1275*4882a593Smuzhiyun 			if (!force_clkinit)
1276*4882a593Smuzhiyun 				dev_info(&slot->mmc->class_dev,
1277*4882a593Smuzhiyun 					 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1278*4882a593Smuzhiyun 					 slot->id, host->bus_hz, clock,
1279*4882a593Smuzhiyun 					 div ? ((host->bus_hz / div) >> 1) :
1280*4882a593Smuzhiyun 					 host->bus_hz, div);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 			/*
1283*4882a593Smuzhiyun 			 * If card is polling, display the message only
1284*4882a593Smuzhiyun 			 * one time at boot time.
1285*4882a593Smuzhiyun 			 */
1286*4882a593Smuzhiyun 			if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1287*4882a593Smuzhiyun 					slot->mmc->f_min == clock)
1288*4882a593Smuzhiyun 				set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1289*4882a593Smuzhiyun 		}
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 		/* disable clock */
1292*4882a593Smuzhiyun 		mci_writel(host, CLKENA, 0);
1293*4882a593Smuzhiyun 		mci_writel(host, CLKSRC, 0);
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 		/* inform CIU */
1296*4882a593Smuzhiyun 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 		/* set clock to desired speed */
1299*4882a593Smuzhiyun 		mci_writel(host, CLKDIV, div);
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 		/* inform CIU */
1302*4882a593Smuzhiyun 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 		/* enable clock; only low power if no SDIO */
1305*4882a593Smuzhiyun 		clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1306*4882a593Smuzhiyun 		if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
1307*4882a593Smuzhiyun 			clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1308*4882a593Smuzhiyun 		mci_writel(host, CLKENA, clk_en_a);
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 		/* inform CIU */
1311*4882a593Smuzhiyun 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 		/* keep the last clock value that was requested from core */
1314*4882a593Smuzhiyun 		slot->__clk_old = clock;
1315*4882a593Smuzhiyun 		slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) :
1316*4882a593Smuzhiyun 					  host->bus_hz;
1317*4882a593Smuzhiyun 	}
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	host->current_speed = clock;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/* Set the current slot bus width */
1322*4882a593Smuzhiyun 	mci_writel(host, CTYPE, (slot->ctype << slot->id));
1323*4882a593Smuzhiyun }
1324*4882a593Smuzhiyun 
__dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot,struct mmc_command * cmd)1325*4882a593Smuzhiyun static void __dw_mci_start_request(struct dw_mci *host,
1326*4882a593Smuzhiyun 				   struct dw_mci_slot *slot,
1327*4882a593Smuzhiyun 				   struct mmc_command *cmd)
1328*4882a593Smuzhiyun {
1329*4882a593Smuzhiyun 	struct mmc_request *mrq;
1330*4882a593Smuzhiyun 	struct mmc_data	*data;
1331*4882a593Smuzhiyun 	u32 cmdflags;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	mrq = slot->mrq;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	host->mrq = mrq;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	host->pending_events = 0;
1338*4882a593Smuzhiyun 	host->completed_events = 0;
1339*4882a593Smuzhiyun 	host->cmd_status = 0;
1340*4882a593Smuzhiyun 	host->data_status = 0;
1341*4882a593Smuzhiyun 	host->dir_status = 0;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	if (host->is_rv1106_sd)
1344*4882a593Smuzhiyun 		mci_writel(host, CTYPE, (slot->ctype << slot->id));
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	data = cmd->data;
1347*4882a593Smuzhiyun 	if (data) {
1348*4882a593Smuzhiyun 		mci_writel(host, TMOUT, 0xFFFFFFFF);
1349*4882a593Smuzhiyun 		if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE))
1350*4882a593Smuzhiyun 			mci_writel(host, BYTCNT, 0);
1351*4882a593Smuzhiyun 		else
1352*4882a593Smuzhiyun 			mci_writel(host, BYTCNT, data->blksz*data->blocks);
1353*4882a593Smuzhiyun 		mci_writel(host, BLKSIZ, data->blksz);
1354*4882a593Smuzhiyun 	}
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	/* this is the first command, send the initialization clock */
1359*4882a593Smuzhiyun 	if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1360*4882a593Smuzhiyun 		cmdflags |= SDMMC_CMD_INIT;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	if (data) {
1363*4882a593Smuzhiyun 		dw_mci_submit_data(host, data);
1364*4882a593Smuzhiyun 		wmb(); /* drain writebuffer */
1365*4882a593Smuzhiyun 	}
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	dw_mci_start_command(host, cmd, cmdflags);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
1370*4882a593Smuzhiyun 		unsigned long irqflags;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 		/*
1373*4882a593Smuzhiyun 		 * Databook says to fail after 2ms w/ no response, but evidence
1374*4882a593Smuzhiyun 		 * shows that sometimes the cmd11 interrupt takes over 130ms.
1375*4882a593Smuzhiyun 		 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1376*4882a593Smuzhiyun 		 * is just about to roll over.
1377*4882a593Smuzhiyun 		 *
1378*4882a593Smuzhiyun 		 * We do this whole thing under spinlock and only if the
1379*4882a593Smuzhiyun 		 * command hasn't already completed (indicating the the irq
1380*4882a593Smuzhiyun 		 * already ran so we don't want the timeout).
1381*4882a593Smuzhiyun 		 */
1382*4882a593Smuzhiyun 		spin_lock_irqsave(&host->irq_lock, irqflags);
1383*4882a593Smuzhiyun 		if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1384*4882a593Smuzhiyun 			mod_timer(&host->cmd11_timer,
1385*4882a593Smuzhiyun 				jiffies + msecs_to_jiffies(500) + 1);
1386*4882a593Smuzhiyun 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
1387*4882a593Smuzhiyun 	}
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot)1392*4882a593Smuzhiyun static void dw_mci_start_request(struct dw_mci *host,
1393*4882a593Smuzhiyun 				 struct dw_mci_slot *slot)
1394*4882a593Smuzhiyun {
1395*4882a593Smuzhiyun 	struct mmc_request *mrq = slot->mrq;
1396*4882a593Smuzhiyun 	struct mmc_command *cmd;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1399*4882a593Smuzhiyun 	__dw_mci_start_request(host, slot, cmd);
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun /* must be called with host->lock held */
dw_mci_queue_request(struct dw_mci * host,struct dw_mci_slot * slot,struct mmc_request * mrq)1403*4882a593Smuzhiyun static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1404*4882a593Smuzhiyun 				 struct mmc_request *mrq)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1407*4882a593Smuzhiyun 		 host->state);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	slot->mrq = mrq;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	if (host->state == STATE_WAITING_CMD11_DONE) {
1412*4882a593Smuzhiyun 		dev_warn(&slot->mmc->class_dev,
1413*4882a593Smuzhiyun 			 "Voltage change didn't complete\n");
1414*4882a593Smuzhiyun 		/*
1415*4882a593Smuzhiyun 		 * this case isn't expected to happen, so we can
1416*4882a593Smuzhiyun 		 * either crash here or just try to continue on
1417*4882a593Smuzhiyun 		 * in the closest possible state
1418*4882a593Smuzhiyun 		 */
1419*4882a593Smuzhiyun 		host->state = STATE_IDLE;
1420*4882a593Smuzhiyun 	}
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	if (host->state == STATE_IDLE) {
1423*4882a593Smuzhiyun 		host->state = STATE_SENDING_CMD;
1424*4882a593Smuzhiyun 		dw_mci_start_request(host, slot);
1425*4882a593Smuzhiyun 	} else {
1426*4882a593Smuzhiyun 		list_add_tail(&slot->queue_node, &host->queue);
1427*4882a593Smuzhiyun 	}
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun static bool dw_mci_reset(struct dw_mci *host);
dw_mci_request(struct mmc_host * mmc,struct mmc_request * mrq)1431*4882a593Smuzhiyun static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1432*4882a593Smuzhiyun {
1433*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1434*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	WARN_ON(slot->mrq);
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	/*
1439*4882a593Smuzhiyun 	 * The check for card presence and queueing of the request must be
1440*4882a593Smuzhiyun 	 * atomic, otherwise the card could be removed in between and the
1441*4882a593Smuzhiyun 	 * request wouldn't fail until another card was inserted.
1442*4882a593Smuzhiyun 	 */
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	if (!dw_mci_get_cd(mmc)) {
1445*4882a593Smuzhiyun 		mrq->cmd->error = -ENOMEDIUM;
1446*4882a593Smuzhiyun 		mmc_request_done(mmc, mrq);
1447*4882a593Smuzhiyun 		return;
1448*4882a593Smuzhiyun 	}
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	if (host->is_rv1106_sd) {
1451*4882a593Smuzhiyun 		u32 reg;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 		readl_poll_timeout(host->regs + SDMMC_STATUS, reg,
1454*4882a593Smuzhiyun 				   reg & BIT(2), USEC_PER_MSEC, 500 * USEC_PER_MSEC);
1455*4882a593Smuzhiyun 	}
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	spin_lock_bh(&host->lock);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	if (host->is_rv1106_sd)
1460*4882a593Smuzhiyun 		dw_mci_reset(host);
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	dw_mci_queue_request(host, slot, mrq);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	spin_unlock_bh(&host->lock);
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun 
dw_mci_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)1467*4882a593Smuzhiyun static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1470*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1471*4882a593Smuzhiyun 	u32 regs;
1472*4882a593Smuzhiyun 	int ret;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	switch (ios->bus_width) {
1475*4882a593Smuzhiyun 	case MMC_BUS_WIDTH_4:
1476*4882a593Smuzhiyun 		slot->ctype = SDMMC_CTYPE_4BIT;
1477*4882a593Smuzhiyun 		break;
1478*4882a593Smuzhiyun 	case MMC_BUS_WIDTH_8:
1479*4882a593Smuzhiyun 		slot->ctype = SDMMC_CTYPE_8BIT;
1480*4882a593Smuzhiyun 		break;
1481*4882a593Smuzhiyun 	default:
1482*4882a593Smuzhiyun 		/* set default 1 bit mode */
1483*4882a593Smuzhiyun 		slot->ctype = SDMMC_CTYPE_1BIT;
1484*4882a593Smuzhiyun 	}
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	regs = mci_readl(slot->host, UHS_REG);
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	/* DDR mode set */
1489*4882a593Smuzhiyun 	if (ios->timing == MMC_TIMING_MMC_DDR52 ||
1490*4882a593Smuzhiyun 	    ios->timing == MMC_TIMING_UHS_DDR50 ||
1491*4882a593Smuzhiyun 	    ios->timing == MMC_TIMING_MMC_HS400)
1492*4882a593Smuzhiyun 		regs |= ((0x1 << slot->id) << 16);
1493*4882a593Smuzhiyun 	else
1494*4882a593Smuzhiyun 		regs &= ~((0x1 << slot->id) << 16);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	mci_writel(slot->host, UHS_REG, regs);
1497*4882a593Smuzhiyun 	slot->host->timing = ios->timing;
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 	/*
1500*4882a593Smuzhiyun 	 * Use mirror of ios->clock to prevent race with mmc
1501*4882a593Smuzhiyun 	 * core ios update when finding the minimum.
1502*4882a593Smuzhiyun 	 */
1503*4882a593Smuzhiyun 	slot->clock = ios->clock;
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	if (drv_data && drv_data->set_ios)
1506*4882a593Smuzhiyun 		drv_data->set_ios(slot->host, ios);
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	switch (ios->power_mode) {
1509*4882a593Smuzhiyun 	case MMC_POWER_UP:
1510*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(slot->host->pinctrl))
1511*4882a593Smuzhiyun 			pinctrl_select_state(slot->host->pinctrl, slot->host->idle_state);
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 		if (!IS_ERR(mmc->supply.vmmc)) {
1514*4882a593Smuzhiyun 			ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1515*4882a593Smuzhiyun 					ios->vdd);
1516*4882a593Smuzhiyun 			if (ret) {
1517*4882a593Smuzhiyun 				dev_err(slot->host->dev,
1518*4882a593Smuzhiyun 					"failed to enable vmmc regulator\n");
1519*4882a593Smuzhiyun 				/*return, if failed turn on vmmc*/
1520*4882a593Smuzhiyun 				return;
1521*4882a593Smuzhiyun 			}
1522*4882a593Smuzhiyun 		}
1523*4882a593Smuzhiyun 		set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1524*4882a593Smuzhiyun 		regs = mci_readl(slot->host, PWREN);
1525*4882a593Smuzhiyun 		regs |= (1 << slot->id);
1526*4882a593Smuzhiyun 		mci_writel(slot->host, PWREN, regs);
1527*4882a593Smuzhiyun 		break;
1528*4882a593Smuzhiyun 	case MMC_POWER_ON:
1529*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(slot->host->pinctrl))
1530*4882a593Smuzhiyun 			pinctrl_select_state(slot->host->pinctrl, slot->host->normal_state);
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 		if (!slot->host->vqmmc_enabled) {
1533*4882a593Smuzhiyun 			if (!IS_ERR(mmc->supply.vqmmc)) {
1534*4882a593Smuzhiyun 				ret = regulator_enable(mmc->supply.vqmmc);
1535*4882a593Smuzhiyun 				if (ret < 0)
1536*4882a593Smuzhiyun 					dev_err(slot->host->dev,
1537*4882a593Smuzhiyun 						"failed to enable vqmmc\n");
1538*4882a593Smuzhiyun 				else
1539*4882a593Smuzhiyun 					slot->host->vqmmc_enabled = true;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 			} else {
1542*4882a593Smuzhiyun 				/* Keep track so we don't reset again */
1543*4882a593Smuzhiyun 				slot->host->vqmmc_enabled = true;
1544*4882a593Smuzhiyun 			}
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun 			/* Reset our state machine after powering on */
1547*4882a593Smuzhiyun 			dw_mci_ctrl_reset(slot->host,
1548*4882a593Smuzhiyun 					  SDMMC_CTRL_ALL_RESET_FLAGS);
1549*4882a593Smuzhiyun 		}
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 		/* Adjust clock / bus width after power is up */
1552*4882a593Smuzhiyun 		dw_mci_setup_bus(slot, false);
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 		break;
1555*4882a593Smuzhiyun 	case MMC_POWER_OFF:
1556*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(slot->host->pinctrl))
1557*4882a593Smuzhiyun 			pinctrl_select_state(slot->host->pinctrl, slot->host->idle_state);
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 		/* Turn clock off before power goes down */
1560*4882a593Smuzhiyun 		dw_mci_setup_bus(slot, false);
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 		if (!IS_ERR(mmc->supply.vmmc))
1563*4882a593Smuzhiyun 			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 		if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
1566*4882a593Smuzhiyun 			regulator_disable(mmc->supply.vqmmc);
1567*4882a593Smuzhiyun 		slot->host->vqmmc_enabled = false;
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 		regs = mci_readl(slot->host, PWREN);
1570*4882a593Smuzhiyun 		regs &= ~(1 << slot->id);
1571*4882a593Smuzhiyun 		mci_writel(slot->host, PWREN, regs);
1572*4882a593Smuzhiyun 		break;
1573*4882a593Smuzhiyun 	default:
1574*4882a593Smuzhiyun 		break;
1575*4882a593Smuzhiyun 	}
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1578*4882a593Smuzhiyun 		slot->host->state = STATE_IDLE;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
dw_mci_card_busy(struct mmc_host * mmc)1581*4882a593Smuzhiyun static int dw_mci_card_busy(struct mmc_host *mmc)
1582*4882a593Smuzhiyun {
1583*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1584*4882a593Smuzhiyun 	u32 status;
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	/*
1587*4882a593Smuzhiyun 	 * Check the busy bit which is low when DAT[3:0]
1588*4882a593Smuzhiyun 	 * (the data lines) are 0000
1589*4882a593Smuzhiyun 	 */
1590*4882a593Smuzhiyun 	status = mci_readl(slot->host, STATUS);
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun 	return !!(status & SDMMC_STATUS_BUSY);
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun 
dw_mci_switch_voltage(struct mmc_host * mmc,struct mmc_ios * ios)1595*4882a593Smuzhiyun static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1596*4882a593Smuzhiyun {
1597*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1598*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1599*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
1600*4882a593Smuzhiyun 	u32 uhs;
1601*4882a593Smuzhiyun 	u32 v18 = SDMMC_UHS_18V << slot->id;
1602*4882a593Smuzhiyun 	int ret;
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	if (drv_data && drv_data->switch_voltage)
1605*4882a593Smuzhiyun 		return drv_data->switch_voltage(mmc, ios);
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	/*
1608*4882a593Smuzhiyun 	 * Program the voltage.  Note that some instances of dw_mmc may use
1609*4882a593Smuzhiyun 	 * the UHS_REG for this.  For other instances (like exynos) the UHS_REG
1610*4882a593Smuzhiyun 	 * does no harm but you need to set the regulator directly.  Try both.
1611*4882a593Smuzhiyun 	 */
1612*4882a593Smuzhiyun 	uhs = mci_readl(host, UHS_REG);
1613*4882a593Smuzhiyun 	if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1614*4882a593Smuzhiyun 		uhs &= ~v18;
1615*4882a593Smuzhiyun 	else
1616*4882a593Smuzhiyun 		uhs |= v18;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	if (!IS_ERR(mmc->supply.vqmmc)) {
1619*4882a593Smuzhiyun 		ret = mmc_regulator_set_vqmmc(mmc, ios);
1620*4882a593Smuzhiyun 		if (ret < 0) {
1621*4882a593Smuzhiyun 			dev_dbg(&mmc->class_dev,
1622*4882a593Smuzhiyun 					 "Regulator set error %d - %s V\n",
1623*4882a593Smuzhiyun 					 ret, uhs & v18 ? "1.8" : "3.3");
1624*4882a593Smuzhiyun 			return ret;
1625*4882a593Smuzhiyun 		}
1626*4882a593Smuzhiyun 	}
1627*4882a593Smuzhiyun 	mci_writel(host, UHS_REG, uhs);
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	return 0;
1630*4882a593Smuzhiyun }
1631*4882a593Smuzhiyun 
dw_mci_get_ro(struct mmc_host * mmc)1632*4882a593Smuzhiyun static int dw_mci_get_ro(struct mmc_host *mmc)
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	int read_only;
1635*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1636*4882a593Smuzhiyun 	int gpio_ro = mmc_gpio_get_ro(mmc);
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 	/* Use platform get_ro function, else try on board write protect */
1639*4882a593Smuzhiyun 	if (gpio_ro >= 0)
1640*4882a593Smuzhiyun 		read_only = gpio_ro;
1641*4882a593Smuzhiyun 	else
1642*4882a593Smuzhiyun 		read_only =
1643*4882a593Smuzhiyun 			mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 	dev_dbg(&mmc->class_dev, "card is %s\n",
1646*4882a593Smuzhiyun 		read_only ? "read-only" : "read-write");
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	return read_only;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun 
dw_mci_hw_reset(struct mmc_host * mmc)1651*4882a593Smuzhiyun static void dw_mci_hw_reset(struct mmc_host *mmc)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1654*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1655*4882a593Smuzhiyun 	int reset;
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	if (host->use_dma == TRANS_MODE_IDMAC)
1658*4882a593Smuzhiyun 		dw_mci_idmac_reset(host);
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1661*4882a593Smuzhiyun 				     SDMMC_CTRL_FIFO_RESET))
1662*4882a593Smuzhiyun 		return;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	/*
1665*4882a593Smuzhiyun 	 * According to eMMC spec, card reset procedure:
1666*4882a593Smuzhiyun 	 * tRstW >= 1us:   RST_n pulse width
1667*4882a593Smuzhiyun 	 * tRSCA >= 200us: RST_n to Command time
1668*4882a593Smuzhiyun 	 * tRSTH >= 1us:   RST_n high period
1669*4882a593Smuzhiyun 	 */
1670*4882a593Smuzhiyun 	reset = mci_readl(host, RST_N);
1671*4882a593Smuzhiyun 	reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1672*4882a593Smuzhiyun 	mci_writel(host, RST_N, reset);
1673*4882a593Smuzhiyun 	usleep_range(1, 2);
1674*4882a593Smuzhiyun 	reset |= SDMMC_RST_HWACTIVE << slot->id;
1675*4882a593Smuzhiyun 	mci_writel(host, RST_N, reset);
1676*4882a593Smuzhiyun 	usleep_range(200, 300);
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun 
dw_mci_init_card(struct mmc_host * mmc,struct mmc_card * card)1679*4882a593Smuzhiyun static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
1680*4882a593Smuzhiyun {
1681*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1682*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 	/*
1685*4882a593Smuzhiyun 	 * Low power mode will stop the card clock when idle.  According to the
1686*4882a593Smuzhiyun 	 * description of the CLKENA register we should disable low power mode
1687*4882a593Smuzhiyun 	 * for SDIO cards if we need SDIO interrupts to work.
1688*4882a593Smuzhiyun 	 */
1689*4882a593Smuzhiyun 	if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1690*4882a593Smuzhiyun 		const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1691*4882a593Smuzhiyun 		u32 clk_en_a_old;
1692*4882a593Smuzhiyun 		u32 clk_en_a;
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 		clk_en_a_old = mci_readl(host, CLKENA);
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun 		if (card->type == MMC_TYPE_SDIO ||
1697*4882a593Smuzhiyun 		    card->type == MMC_TYPE_SD_COMBO) {
1698*4882a593Smuzhiyun 			set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1699*4882a593Smuzhiyun 			clk_en_a = clk_en_a_old & ~clken_low_pwr;
1700*4882a593Smuzhiyun 		} else {
1701*4882a593Smuzhiyun 			clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1702*4882a593Smuzhiyun 			clk_en_a = clk_en_a_old | clken_low_pwr;
1703*4882a593Smuzhiyun 		}
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 		if (clk_en_a != clk_en_a_old) {
1706*4882a593Smuzhiyun 			mci_writel(host, CLKENA, clk_en_a);
1707*4882a593Smuzhiyun 			mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1708*4882a593Smuzhiyun 				     SDMMC_CMD_PRV_DAT_WAIT, 0);
1709*4882a593Smuzhiyun 		}
1710*4882a593Smuzhiyun 	}
1711*4882a593Smuzhiyun }
1712*4882a593Smuzhiyun 
__dw_mci_enable_sdio_irq(struct dw_mci_slot * slot,int enb)1713*4882a593Smuzhiyun static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
1714*4882a593Smuzhiyun {
1715*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1716*4882a593Smuzhiyun 	unsigned long irqflags;
1717*4882a593Smuzhiyun 	u32 int_mask;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	/* Enable/disable Slot Specific SDIO interrupt */
1722*4882a593Smuzhiyun 	int_mask = mci_readl(host, INTMASK);
1723*4882a593Smuzhiyun 	if (enb)
1724*4882a593Smuzhiyun 		int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1725*4882a593Smuzhiyun 	else
1726*4882a593Smuzhiyun 		int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1727*4882a593Smuzhiyun 	mci_writel(host, INTMASK, int_mask);
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun 
dw_mci_enable_sdio_irq(struct mmc_host * mmc,int enb)1732*4882a593Smuzhiyun static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1733*4882a593Smuzhiyun {
1734*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1735*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	__dw_mci_enable_sdio_irq(slot, enb);
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	/* Avoid runtime suspending the device when SDIO IRQ is enabled */
1740*4882a593Smuzhiyun 	if (enb)
1741*4882a593Smuzhiyun 		pm_runtime_get_noresume(host->dev);
1742*4882a593Smuzhiyun 	else
1743*4882a593Smuzhiyun 		pm_runtime_put_noidle(host->dev);
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun 
dw_mci_ack_sdio_irq(struct mmc_host * mmc)1746*4882a593Smuzhiyun static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
1747*4882a593Smuzhiyun {
1748*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 	__dw_mci_enable_sdio_irq(slot, 1);
1751*4882a593Smuzhiyun }
1752*4882a593Smuzhiyun 
dw_mci_execute_tuning(struct mmc_host * mmc,u32 opcode)1753*4882a593Smuzhiyun static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1754*4882a593Smuzhiyun {
1755*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1756*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1757*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
1758*4882a593Smuzhiyun 	int err = -EINVAL;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	if (drv_data && drv_data->execute_tuning)
1761*4882a593Smuzhiyun 		err = drv_data->execute_tuning(slot, opcode);
1762*4882a593Smuzhiyun 	return err;
1763*4882a593Smuzhiyun }
1764*4882a593Smuzhiyun 
dw_mci_prepare_hs400_tuning(struct mmc_host * mmc,struct mmc_ios * ios)1765*4882a593Smuzhiyun static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1766*4882a593Smuzhiyun 				       struct mmc_ios *ios)
1767*4882a593Smuzhiyun {
1768*4882a593Smuzhiyun 	struct dw_mci_slot *slot = mmc_priv(mmc);
1769*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
1770*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	if (drv_data && drv_data->prepare_hs400_tuning)
1773*4882a593Smuzhiyun 		return drv_data->prepare_hs400_tuning(host, ios);
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 	return 0;
1776*4882a593Smuzhiyun }
1777*4882a593Smuzhiyun 
dw_mci_reset(struct dw_mci * host)1778*4882a593Smuzhiyun static bool dw_mci_reset(struct dw_mci *host)
1779*4882a593Smuzhiyun {
1780*4882a593Smuzhiyun 	u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
1781*4882a593Smuzhiyun 	bool ret = false;
1782*4882a593Smuzhiyun 	u32 status = 0;
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	/*
1785*4882a593Smuzhiyun 	 * Resetting generates a block interrupt, hence setting
1786*4882a593Smuzhiyun 	 * the scatter-gather pointer to NULL.
1787*4882a593Smuzhiyun 	 */
1788*4882a593Smuzhiyun 	if (host->sg) {
1789*4882a593Smuzhiyun 		sg_miter_stop(&host->sg_miter);
1790*4882a593Smuzhiyun 		host->sg = NULL;
1791*4882a593Smuzhiyun 	}
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	if (host->use_dma)
1794*4882a593Smuzhiyun 		flags |= SDMMC_CTRL_DMA_RESET;
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	if (dw_mci_ctrl_reset(host, flags)) {
1797*4882a593Smuzhiyun 		/*
1798*4882a593Smuzhiyun 		 * In all cases we clear the RAWINTS
1799*4882a593Smuzhiyun 		 * register to clear any interrupts.
1800*4882a593Smuzhiyun 		 */
1801*4882a593Smuzhiyun 		mci_writel(host, RINTSTS, 0xFFFFFFFF);
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun 		if (!host->use_dma) {
1804*4882a593Smuzhiyun 			ret = true;
1805*4882a593Smuzhiyun 			goto ciu_out;
1806*4882a593Smuzhiyun 		}
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 		/* Wait for dma_req to be cleared */
1809*4882a593Smuzhiyun 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
1810*4882a593Smuzhiyun 					      status,
1811*4882a593Smuzhiyun 					      !(status & SDMMC_STATUS_DMA_REQ),
1812*4882a593Smuzhiyun 					      1, 500 * USEC_PER_MSEC)) {
1813*4882a593Smuzhiyun 			dev_err(host->dev,
1814*4882a593Smuzhiyun 				"%s: Timeout waiting for dma_req to be cleared\n",
1815*4882a593Smuzhiyun 				__func__);
1816*4882a593Smuzhiyun 			goto ciu_out;
1817*4882a593Smuzhiyun 		}
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 		/* when using DMA next we reset the fifo again */
1820*4882a593Smuzhiyun 		if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
1821*4882a593Smuzhiyun 			goto ciu_out;
1822*4882a593Smuzhiyun 	} else {
1823*4882a593Smuzhiyun 		/* if the controller reset bit did clear, then set clock regs */
1824*4882a593Smuzhiyun 		if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
1825*4882a593Smuzhiyun 			dev_err(host->dev,
1826*4882a593Smuzhiyun 				"%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
1827*4882a593Smuzhiyun 				__func__);
1828*4882a593Smuzhiyun 			goto ciu_out;
1829*4882a593Smuzhiyun 		}
1830*4882a593Smuzhiyun 	}
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 	if (host->use_dma == TRANS_MODE_IDMAC)
1833*4882a593Smuzhiyun 		/* It is also required that we reinit idmac */
1834*4882a593Smuzhiyun 		dw_mci_idmac_init(host);
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	ret = true;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun ciu_out:
1839*4882a593Smuzhiyun 	/* After a CTRL reset we need to have CIU set clock registers  */
1840*4882a593Smuzhiyun 	mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0);
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	return ret;
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun static const struct mmc_host_ops dw_mci_ops = {
1846*4882a593Smuzhiyun 	.request		= dw_mci_request,
1847*4882a593Smuzhiyun 	.pre_req		= dw_mci_pre_req,
1848*4882a593Smuzhiyun 	.post_req		= dw_mci_post_req,
1849*4882a593Smuzhiyun 	.set_ios		= dw_mci_set_ios,
1850*4882a593Smuzhiyun 	.get_ro			= dw_mci_get_ro,
1851*4882a593Smuzhiyun 	.get_cd			= dw_mci_get_cd,
1852*4882a593Smuzhiyun 	.hw_reset               = dw_mci_hw_reset,
1853*4882a593Smuzhiyun 	.enable_sdio_irq	= dw_mci_enable_sdio_irq,
1854*4882a593Smuzhiyun 	.ack_sdio_irq		= dw_mci_ack_sdio_irq,
1855*4882a593Smuzhiyun 	.execute_tuning		= dw_mci_execute_tuning,
1856*4882a593Smuzhiyun 	.card_busy		= dw_mci_card_busy,
1857*4882a593Smuzhiyun 	.start_signal_voltage_switch = dw_mci_switch_voltage,
1858*4882a593Smuzhiyun 	.init_card		= dw_mci_init_card,
1859*4882a593Smuzhiyun 	.prepare_hs400_tuning	= dw_mci_prepare_hs400_tuning,
1860*4882a593Smuzhiyun };
1861*4882a593Smuzhiyun 
dw_mci_request_end(struct dw_mci * host,struct mmc_request * mrq)1862*4882a593Smuzhiyun static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1863*4882a593Smuzhiyun 	__releases(&host->lock)
1864*4882a593Smuzhiyun 	__acquires(&host->lock)
1865*4882a593Smuzhiyun {
1866*4882a593Smuzhiyun 	struct dw_mci_slot *slot;
1867*4882a593Smuzhiyun 	struct mmc_host	*prev_mmc = host->slot->mmc;
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	WARN_ON(host->cmd || host->data);
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	host->slot->mrq = NULL;
1872*4882a593Smuzhiyun 	host->mrq = NULL;
1873*4882a593Smuzhiyun 	if (!list_empty(&host->queue)) {
1874*4882a593Smuzhiyun 		slot = list_entry(host->queue.next,
1875*4882a593Smuzhiyun 				  struct dw_mci_slot, queue_node);
1876*4882a593Smuzhiyun 		list_del(&slot->queue_node);
1877*4882a593Smuzhiyun 		dev_vdbg(host->dev, "list not empty: %s is next\n",
1878*4882a593Smuzhiyun 			 mmc_hostname(slot->mmc));
1879*4882a593Smuzhiyun 		host->state = STATE_SENDING_CMD;
1880*4882a593Smuzhiyun 		dw_mci_start_request(host, slot);
1881*4882a593Smuzhiyun 	} else {
1882*4882a593Smuzhiyun 		dev_vdbg(host->dev, "list empty\n");
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 		if (host->state == STATE_SENDING_CMD11)
1885*4882a593Smuzhiyun 			host->state = STATE_WAITING_CMD11_DONE;
1886*4882a593Smuzhiyun 		else
1887*4882a593Smuzhiyun 			host->state = STATE_IDLE;
1888*4882a593Smuzhiyun 	}
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	spin_unlock(&host->lock);
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun 	mmc_request_done(prev_mmc, mrq);
1893*4882a593Smuzhiyun 	spin_lock(&host->lock);
1894*4882a593Smuzhiyun }
1895*4882a593Smuzhiyun 
dw_mci_command_complete(struct dw_mci * host,struct mmc_command * cmd)1896*4882a593Smuzhiyun static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun 	u32 status = host->cmd_status;
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun 	host->cmd_status = 0;
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	/* Read the response from the card (up to 16 bytes) */
1903*4882a593Smuzhiyun 	if (cmd->flags & MMC_RSP_PRESENT) {
1904*4882a593Smuzhiyun 		if (cmd->flags & MMC_RSP_136) {
1905*4882a593Smuzhiyun 			cmd->resp[3] = mci_readl(host, RESP0);
1906*4882a593Smuzhiyun 			cmd->resp[2] = mci_readl(host, RESP1);
1907*4882a593Smuzhiyun 			cmd->resp[1] = mci_readl(host, RESP2);
1908*4882a593Smuzhiyun 			cmd->resp[0] = mci_readl(host, RESP3);
1909*4882a593Smuzhiyun 		} else {
1910*4882a593Smuzhiyun 			cmd->resp[0] = mci_readl(host, RESP0);
1911*4882a593Smuzhiyun 			cmd->resp[1] = 0;
1912*4882a593Smuzhiyun 			cmd->resp[2] = 0;
1913*4882a593Smuzhiyun 			cmd->resp[3] = 0;
1914*4882a593Smuzhiyun 		}
1915*4882a593Smuzhiyun 	}
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	if (status & SDMMC_INT_RTO)
1918*4882a593Smuzhiyun 		cmd->error = -ETIMEDOUT;
1919*4882a593Smuzhiyun 	else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1920*4882a593Smuzhiyun 		cmd->error = -EILSEQ;
1921*4882a593Smuzhiyun 	else if (status & SDMMC_INT_RESP_ERR)
1922*4882a593Smuzhiyun 		cmd->error = -EIO;
1923*4882a593Smuzhiyun 	else
1924*4882a593Smuzhiyun 		cmd->error = 0;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	return cmd->error;
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun 
dw_mci_data_complete(struct dw_mci * host,struct mmc_data * data)1929*4882a593Smuzhiyun static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1930*4882a593Smuzhiyun {
1931*4882a593Smuzhiyun 	u32 status = host->data_status;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE) && (status & SDMMC_INT_DATA_OVER))
1934*4882a593Smuzhiyun 		goto finish;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	if (status & DW_MCI_DATA_ERROR_FLAGS) {
1937*4882a593Smuzhiyun 		if (status & SDMMC_INT_DRTO) {
1938*4882a593Smuzhiyun 			data->error = -ETIMEDOUT;
1939*4882a593Smuzhiyun 		} else if (status & SDMMC_INT_DCRC) {
1940*4882a593Smuzhiyun 			data->error = -EILSEQ;
1941*4882a593Smuzhiyun 		} else if (status & SDMMC_INT_EBE) {
1942*4882a593Smuzhiyun 			if (host->dir_status ==
1943*4882a593Smuzhiyun 				DW_MCI_SEND_STATUS) {
1944*4882a593Smuzhiyun 				/*
1945*4882a593Smuzhiyun 				 * No data CRC status was returned.
1946*4882a593Smuzhiyun 				 * The number of bytes transferred
1947*4882a593Smuzhiyun 				 * will be exaggerated in PIO mode.
1948*4882a593Smuzhiyun 				 */
1949*4882a593Smuzhiyun 				data->bytes_xfered = 0;
1950*4882a593Smuzhiyun 				data->error = -ETIMEDOUT;
1951*4882a593Smuzhiyun 			} else if (host->dir_status ==
1952*4882a593Smuzhiyun 					DW_MCI_RECV_STATUS) {
1953*4882a593Smuzhiyun 				data->error = -EILSEQ;
1954*4882a593Smuzhiyun 			}
1955*4882a593Smuzhiyun 		} else {
1956*4882a593Smuzhiyun 			/* SDMMC_INT_SBE is included */
1957*4882a593Smuzhiyun 			data->error = -EILSEQ;
1958*4882a593Smuzhiyun 		}
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 		dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 		/*
1963*4882a593Smuzhiyun 		 * After an error, there may be data lingering
1964*4882a593Smuzhiyun 		 * in the FIFO
1965*4882a593Smuzhiyun 		 */
1966*4882a593Smuzhiyun 		dw_mci_reset(host);
1967*4882a593Smuzhiyun 	} else {
1968*4882a593Smuzhiyun finish:
1969*4882a593Smuzhiyun 		data->bytes_xfered = data->blocks * data->blksz;
1970*4882a593Smuzhiyun 		data->error = 0;
1971*4882a593Smuzhiyun 	}
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	return data->error;
1974*4882a593Smuzhiyun }
1975*4882a593Smuzhiyun 
dw_mci_set_drto(struct dw_mci * host)1976*4882a593Smuzhiyun static void dw_mci_set_drto(struct dw_mci *host)
1977*4882a593Smuzhiyun {
1978*4882a593Smuzhiyun 	unsigned int drto_clks;
1979*4882a593Smuzhiyun 	unsigned int drto_div;
1980*4882a593Smuzhiyun 	unsigned int drto_ms;
1981*4882a593Smuzhiyun 	unsigned long irqflags;
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	drto_clks = mci_readl(host, TMOUT) >> 8;
1984*4882a593Smuzhiyun 	drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
1985*4882a593Smuzhiyun 	if (drto_div == 0)
1986*4882a593Smuzhiyun 		drto_div = 1;
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 	drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
1989*4882a593Smuzhiyun 				   host->bus_hz);
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 	/* add a bit spare time */
1992*4882a593Smuzhiyun 	drto_ms += 10;
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
1995*4882a593Smuzhiyun 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
1996*4882a593Smuzhiyun 		mod_timer(&host->dto_timer,
1997*4882a593Smuzhiyun 			  jiffies + msecs_to_jiffies(drto_ms));
1998*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun 
dw_mci_set_xfer_timeout(struct dw_mci * host)2001*4882a593Smuzhiyun static void dw_mci_set_xfer_timeout(struct dw_mci *host)
2002*4882a593Smuzhiyun {
2003*4882a593Smuzhiyun 	unsigned int xfer_clks;
2004*4882a593Smuzhiyun 	unsigned int xfer_div;
2005*4882a593Smuzhiyun 	unsigned int xfer_ms;
2006*4882a593Smuzhiyun 	unsigned long irqflags;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	xfer_clks = mci_readl(host, TMOUT) >> 8;
2009*4882a593Smuzhiyun 	xfer_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
2010*4882a593Smuzhiyun 	if (xfer_div == 0)
2011*4882a593Smuzhiyun 		xfer_div = 1;
2012*4882a593Smuzhiyun 	xfer_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * xfer_clks * xfer_div,
2013*4882a593Smuzhiyun 				   host->bus_hz);
2014*4882a593Smuzhiyun 
2015*4882a593Smuzhiyun 	/* add a bit spare time */
2016*4882a593Smuzhiyun 	xfer_ms += 100;
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
2019*4882a593Smuzhiyun 	if (!test_bit(EVENT_XFER_COMPLETE, &host->pending_events))
2020*4882a593Smuzhiyun 		mod_timer(&host->xfer_timer,
2021*4882a593Smuzhiyun 			  jiffies + msecs_to_jiffies(xfer_ms));
2022*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
2023*4882a593Smuzhiyun }
2024*4882a593Smuzhiyun 
dw_mci_clear_pending_cmd_complete(struct dw_mci * host)2025*4882a593Smuzhiyun static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)
2026*4882a593Smuzhiyun {
2027*4882a593Smuzhiyun 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
2028*4882a593Smuzhiyun 		return false;
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	/*
2031*4882a593Smuzhiyun 	 * Really be certain that the timer has stopped.  This is a bit of
2032*4882a593Smuzhiyun 	 * paranoia and could only really happen if we had really bad
2033*4882a593Smuzhiyun 	 * interrupt latency and the interrupt routine and timeout were
2034*4882a593Smuzhiyun 	 * running concurrently so that the del_timer() in the interrupt
2035*4882a593Smuzhiyun 	 * handler couldn't run.
2036*4882a593Smuzhiyun 	 */
2037*4882a593Smuzhiyun 	WARN_ON(del_timer_sync(&host->cto_timer));
2038*4882a593Smuzhiyun 	clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	return true;
2041*4882a593Smuzhiyun }
2042*4882a593Smuzhiyun 
dw_mci_clear_pending_data_complete(struct dw_mci * host)2043*4882a593Smuzhiyun static bool dw_mci_clear_pending_data_complete(struct dw_mci *host)
2044*4882a593Smuzhiyun {
2045*4882a593Smuzhiyun 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
2046*4882a593Smuzhiyun 		return false;
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	/* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */
2049*4882a593Smuzhiyun 	WARN_ON(del_timer_sync(&host->dto_timer));
2050*4882a593Smuzhiyun 	clear_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	return true;
2053*4882a593Smuzhiyun }
2054*4882a593Smuzhiyun 
dw_mci_tasklet_func(unsigned long priv)2055*4882a593Smuzhiyun static void dw_mci_tasklet_func(unsigned long priv)
2056*4882a593Smuzhiyun {
2057*4882a593Smuzhiyun 	struct dw_mci *host = (struct dw_mci *)priv;
2058*4882a593Smuzhiyun 	struct mmc_data	*data;
2059*4882a593Smuzhiyun 	struct mmc_command *cmd;
2060*4882a593Smuzhiyun 	struct mmc_request *mrq;
2061*4882a593Smuzhiyun 	enum dw_mci_state state;
2062*4882a593Smuzhiyun 	enum dw_mci_state prev_state;
2063*4882a593Smuzhiyun 	unsigned int err;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 	spin_lock(&host->lock);
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	state = host->state;
2068*4882a593Smuzhiyun 	data = host->data;
2069*4882a593Smuzhiyun 	mrq = host->mrq;
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 	do {
2072*4882a593Smuzhiyun 		prev_state = state;
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 		switch (state) {
2075*4882a593Smuzhiyun 		case STATE_IDLE:
2076*4882a593Smuzhiyun 		case STATE_WAITING_CMD11_DONE:
2077*4882a593Smuzhiyun 			break;
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 		case STATE_SENDING_CMD11:
2080*4882a593Smuzhiyun 		case STATE_SENDING_CMD:
2081*4882a593Smuzhiyun 			if (!dw_mci_clear_pending_cmd_complete(host))
2082*4882a593Smuzhiyun 				break;
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 			cmd = host->cmd;
2085*4882a593Smuzhiyun 			host->cmd = NULL;
2086*4882a593Smuzhiyun 			set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
2087*4882a593Smuzhiyun 			err = dw_mci_command_complete(host, cmd);
2088*4882a593Smuzhiyun 			if (cmd == mrq->sbc && !err) {
2089*4882a593Smuzhiyun 				__dw_mci_start_request(host, host->slot,
2090*4882a593Smuzhiyun 						       mrq->cmd);
2091*4882a593Smuzhiyun 				goto unlock;
2092*4882a593Smuzhiyun 			}
2093*4882a593Smuzhiyun 
2094*4882a593Smuzhiyun 			if (cmd->data && err) {
2095*4882a593Smuzhiyun 				/*
2096*4882a593Smuzhiyun 				 * During UHS tuning sequence, sending the stop
2097*4882a593Smuzhiyun 				 * command after the response CRC error would
2098*4882a593Smuzhiyun 				 * throw the system into a confused state
2099*4882a593Smuzhiyun 				 * causing all future tuning phases to report
2100*4882a593Smuzhiyun 				 * failure.
2101*4882a593Smuzhiyun 				 *
2102*4882a593Smuzhiyun 				 * In such case controller will move into a data
2103*4882a593Smuzhiyun 				 * transfer state after a response error or
2104*4882a593Smuzhiyun 				 * response CRC error. Let's let that finish
2105*4882a593Smuzhiyun 				 * before trying to send a stop, so we'll go to
2106*4882a593Smuzhiyun 				 * STATE_SENDING_DATA.
2107*4882a593Smuzhiyun 				 *
2108*4882a593Smuzhiyun 				 * Although letting the data transfer take place
2109*4882a593Smuzhiyun 				 * will waste a bit of time (we already know
2110*4882a593Smuzhiyun 				 * the command was bad), it can't cause any
2111*4882a593Smuzhiyun 				 * errors since it's possible it would have
2112*4882a593Smuzhiyun 				 * taken place anyway if this tasklet got
2113*4882a593Smuzhiyun 				 * delayed. Allowing the transfer to take place
2114*4882a593Smuzhiyun 				 * avoids races and keeps things simple.
2115*4882a593Smuzhiyun 				 */
2116*4882a593Smuzhiyun 				if (err != -ETIMEDOUT &&
2117*4882a593Smuzhiyun 				    host->dir_status == DW_MCI_RECV_STATUS) {
2118*4882a593Smuzhiyun 					state = STATE_SENDING_DATA;
2119*4882a593Smuzhiyun 					continue;
2120*4882a593Smuzhiyun 				}
2121*4882a593Smuzhiyun 
2122*4882a593Smuzhiyun 				send_stop_abort(host, data);
2123*4882a593Smuzhiyun 				dw_mci_stop_dma(host);
2124*4882a593Smuzhiyun 				state = STATE_SENDING_STOP;
2125*4882a593Smuzhiyun 				break;
2126*4882a593Smuzhiyun 			}
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 			if (!cmd->data || err) {
2129*4882a593Smuzhiyun 				dw_mci_request_end(host, mrq);
2130*4882a593Smuzhiyun 				goto unlock;
2131*4882a593Smuzhiyun 			}
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 			prev_state = state = STATE_SENDING_DATA;
2134*4882a593Smuzhiyun 			fallthrough;
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun 		case STATE_SENDING_DATA:
2137*4882a593Smuzhiyun 			/*
2138*4882a593Smuzhiyun 			 * We could get a data error and never a transfer
2139*4882a593Smuzhiyun 			 * complete so we'd better check for it here.
2140*4882a593Smuzhiyun 			 *
2141*4882a593Smuzhiyun 			 * Note that we don't really care if we also got a
2142*4882a593Smuzhiyun 			 * transfer complete; stopping the DMA and sending an
2143*4882a593Smuzhiyun 			 * abort won't hurt.
2144*4882a593Smuzhiyun 			 */
2145*4882a593Smuzhiyun 			if (test_and_clear_bit(EVENT_DATA_ERROR,
2146*4882a593Smuzhiyun 					       &host->pending_events)) {
2147*4882a593Smuzhiyun 				if (!(host->data_status & (SDMMC_INT_DRTO |
2148*4882a593Smuzhiyun 							   SDMMC_INT_EBE)))
2149*4882a593Smuzhiyun 					send_stop_abort(host, data);
2150*4882a593Smuzhiyun 				dw_mci_stop_dma(host);
2151*4882a593Smuzhiyun 				state = STATE_DATA_ERROR;
2152*4882a593Smuzhiyun 				break;
2153*4882a593Smuzhiyun 			}
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2156*4882a593Smuzhiyun 						&host->pending_events)) {
2157*4882a593Smuzhiyun 				/*
2158*4882a593Smuzhiyun 				 * If all data-related interrupts don't come
2159*4882a593Smuzhiyun 				 * within the given time in reading data state.
2160*4882a593Smuzhiyun 				 */
2161*4882a593Smuzhiyun 				if (host->dir_status == DW_MCI_RECV_STATUS)
2162*4882a593Smuzhiyun 					dw_mci_set_drto(host);
2163*4882a593Smuzhiyun 				if (host->need_xfer_timer &&
2164*4882a593Smuzhiyun 				    host->dir_status == DW_MCI_RECV_STATUS)
2165*4882a593Smuzhiyun 					dw_mci_set_xfer_timeout(host);
2166*4882a593Smuzhiyun 				break;
2167*4882a593Smuzhiyun 			}
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 			set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun 			/*
2172*4882a593Smuzhiyun 			 * Handle an EVENT_DATA_ERROR that might have shown up
2173*4882a593Smuzhiyun 			 * before the transfer completed.  This might not have
2174*4882a593Smuzhiyun 			 * been caught by the check above because the interrupt
2175*4882a593Smuzhiyun 			 * could have gone off between the previous check and
2176*4882a593Smuzhiyun 			 * the check for transfer complete.
2177*4882a593Smuzhiyun 			 *
2178*4882a593Smuzhiyun 			 * Technically this ought not be needed assuming we
2179*4882a593Smuzhiyun 			 * get a DATA_COMPLETE eventually (we'll notice the
2180*4882a593Smuzhiyun 			 * error and end the request), but it shouldn't hurt.
2181*4882a593Smuzhiyun 			 *
2182*4882a593Smuzhiyun 			 * This has the advantage of sending the stop command.
2183*4882a593Smuzhiyun 			 */
2184*4882a593Smuzhiyun 			if (test_and_clear_bit(EVENT_DATA_ERROR,
2185*4882a593Smuzhiyun 					       &host->pending_events)) {
2186*4882a593Smuzhiyun 				if (!(host->data_status & (SDMMC_INT_DRTO |
2187*4882a593Smuzhiyun 							   SDMMC_INT_EBE)))
2188*4882a593Smuzhiyun 					send_stop_abort(host, data);
2189*4882a593Smuzhiyun 				dw_mci_stop_dma(host);
2190*4882a593Smuzhiyun 				state = STATE_DATA_ERROR;
2191*4882a593Smuzhiyun 				break;
2192*4882a593Smuzhiyun 			}
2193*4882a593Smuzhiyun 			prev_state = state = STATE_DATA_BUSY;
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 			fallthrough;
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 		case STATE_DATA_BUSY:
2198*4882a593Smuzhiyun 			if (!dw_mci_clear_pending_data_complete(host)) {
2199*4882a593Smuzhiyun 				/*
2200*4882a593Smuzhiyun 				 * If data error interrupt comes but data over
2201*4882a593Smuzhiyun 				 * interrupt doesn't come within the given time.
2202*4882a593Smuzhiyun 				 * in reading data state.
2203*4882a593Smuzhiyun 				 */
2204*4882a593Smuzhiyun 				if (host->dir_status == DW_MCI_RECV_STATUS)
2205*4882a593Smuzhiyun 					dw_mci_set_drto(host);
2206*4882a593Smuzhiyun 				break;
2207*4882a593Smuzhiyun 			}
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 			host->data = NULL;
2210*4882a593Smuzhiyun 			set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2211*4882a593Smuzhiyun 			err = dw_mci_data_complete(host, data);
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 			if (!err) {
2214*4882a593Smuzhiyun 				if (!data->stop || mrq->sbc) {
2215*4882a593Smuzhiyun 					if (mrq->sbc && data->stop)
2216*4882a593Smuzhiyun 						data->stop->error = 0;
2217*4882a593Smuzhiyun 					dw_mci_request_end(host, mrq);
2218*4882a593Smuzhiyun 					goto unlock;
2219*4882a593Smuzhiyun 				}
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 				/* stop command for open-ended transfer*/
2222*4882a593Smuzhiyun 				if (data->stop)
2223*4882a593Smuzhiyun 					send_stop_abort(host, data);
2224*4882a593Smuzhiyun 			} else {
2225*4882a593Smuzhiyun 				/*
2226*4882a593Smuzhiyun 				 * If we don't have a command complete now we'll
2227*4882a593Smuzhiyun 				 * never get one since we just reset everything;
2228*4882a593Smuzhiyun 				 * better end the request.
2229*4882a593Smuzhiyun 				 *
2230*4882a593Smuzhiyun 				 * If we do have a command complete we'll fall
2231*4882a593Smuzhiyun 				 * through to the SENDING_STOP command and
2232*4882a593Smuzhiyun 				 * everything will be peachy keen.
2233*4882a593Smuzhiyun 				 */
2234*4882a593Smuzhiyun 				if (!test_bit(EVENT_CMD_COMPLETE,
2235*4882a593Smuzhiyun 					      &host->pending_events)) {
2236*4882a593Smuzhiyun 					host->cmd = NULL;
2237*4882a593Smuzhiyun 					dw_mci_request_end(host, mrq);
2238*4882a593Smuzhiyun 					goto unlock;
2239*4882a593Smuzhiyun 				}
2240*4882a593Smuzhiyun 			}
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 			/*
2243*4882a593Smuzhiyun 			 * If err has non-zero,
2244*4882a593Smuzhiyun 			 * stop-abort command has been already issued.
2245*4882a593Smuzhiyun 			 */
2246*4882a593Smuzhiyun 			prev_state = state = STATE_SENDING_STOP;
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 			fallthrough;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 		case STATE_SENDING_STOP:
2251*4882a593Smuzhiyun 			if (!dw_mci_clear_pending_cmd_complete(host))
2252*4882a593Smuzhiyun 				break;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 			/* CMD error in data command */
2255*4882a593Smuzhiyun 			if (mrq->cmd->error && mrq->data)
2256*4882a593Smuzhiyun 				dw_mci_reset(host);
2257*4882a593Smuzhiyun 
2258*4882a593Smuzhiyun 			host->cmd = NULL;
2259*4882a593Smuzhiyun 			host->data = NULL;
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 			if (!mrq->sbc && mrq->stop)
2262*4882a593Smuzhiyun 				dw_mci_command_complete(host, mrq->stop);
2263*4882a593Smuzhiyun 			else
2264*4882a593Smuzhiyun 				host->cmd_status = 0;
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun 			dw_mci_request_end(host, mrq);
2267*4882a593Smuzhiyun 			goto unlock;
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 		case STATE_DATA_ERROR:
2270*4882a593Smuzhiyun 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2271*4882a593Smuzhiyun 						&host->pending_events))
2272*4882a593Smuzhiyun 				break;
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 			state = STATE_DATA_BUSY;
2275*4882a593Smuzhiyun 			break;
2276*4882a593Smuzhiyun 		}
2277*4882a593Smuzhiyun 	} while (state != prev_state);
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun 	host->state = state;
2280*4882a593Smuzhiyun unlock:
2281*4882a593Smuzhiyun 	spin_unlock(&host->lock);
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun }
2284*4882a593Smuzhiyun 
2285*4882a593Smuzhiyun /* push final bytes to part_buf, only use during push */
dw_mci_set_part_bytes(struct dw_mci * host,void * buf,int cnt)2286*4882a593Smuzhiyun static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2287*4882a593Smuzhiyun {
2288*4882a593Smuzhiyun 	memcpy((void *)&host->part_buf, buf, cnt);
2289*4882a593Smuzhiyun 	host->part_buf_count = cnt;
2290*4882a593Smuzhiyun }
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun /* append bytes to part_buf, only use during push */
dw_mci_push_part_bytes(struct dw_mci * host,void * buf,int cnt)2293*4882a593Smuzhiyun static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2294*4882a593Smuzhiyun {
2295*4882a593Smuzhiyun 	cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2296*4882a593Smuzhiyun 	memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2297*4882a593Smuzhiyun 	host->part_buf_count += cnt;
2298*4882a593Smuzhiyun 	return cnt;
2299*4882a593Smuzhiyun }
2300*4882a593Smuzhiyun 
2301*4882a593Smuzhiyun /* pull first bytes from part_buf, only use during pull */
dw_mci_pull_part_bytes(struct dw_mci * host,void * buf,int cnt)2302*4882a593Smuzhiyun static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2303*4882a593Smuzhiyun {
2304*4882a593Smuzhiyun 	cnt = min_t(int, cnt, host->part_buf_count);
2305*4882a593Smuzhiyun 	if (cnt) {
2306*4882a593Smuzhiyun 		memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2307*4882a593Smuzhiyun 		       cnt);
2308*4882a593Smuzhiyun 		host->part_buf_count -= cnt;
2309*4882a593Smuzhiyun 		host->part_buf_start += cnt;
2310*4882a593Smuzhiyun 	}
2311*4882a593Smuzhiyun 	return cnt;
2312*4882a593Smuzhiyun }
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun /* 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)2315*4882a593Smuzhiyun static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2316*4882a593Smuzhiyun {
2317*4882a593Smuzhiyun 	memcpy(buf, &host->part_buf, cnt);
2318*4882a593Smuzhiyun 	host->part_buf_start = cnt;
2319*4882a593Smuzhiyun 	host->part_buf_count = (1 << host->data_shift) - cnt;
2320*4882a593Smuzhiyun }
2321*4882a593Smuzhiyun 
dw_mci_push_data16(struct dw_mci * host,void * buf,int cnt)2322*4882a593Smuzhiyun static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2323*4882a593Smuzhiyun {
2324*4882a593Smuzhiyun 	struct mmc_data *data = host->data;
2325*4882a593Smuzhiyun 	int init_cnt = cnt;
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	/* try and push anything in the part_buf */
2328*4882a593Smuzhiyun 	if (unlikely(host->part_buf_count)) {
2329*4882a593Smuzhiyun 		int len = dw_mci_push_part_bytes(host, buf, cnt);
2330*4882a593Smuzhiyun 
2331*4882a593Smuzhiyun 		buf += len;
2332*4882a593Smuzhiyun 		cnt -= len;
2333*4882a593Smuzhiyun 		if (host->part_buf_count == 2) {
2334*4882a593Smuzhiyun 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
2335*4882a593Smuzhiyun 			host->part_buf_count = 0;
2336*4882a593Smuzhiyun 		}
2337*4882a593Smuzhiyun 	}
2338*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2339*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x1)) {
2340*4882a593Smuzhiyun 		while (cnt >= 2) {
2341*4882a593Smuzhiyun 			u16 aligned_buf[64];
2342*4882a593Smuzhiyun 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
2343*4882a593Smuzhiyun 			int items = len >> 1;
2344*4882a593Smuzhiyun 			int i;
2345*4882a593Smuzhiyun 			/* memcpy from input buffer into aligned buffer */
2346*4882a593Smuzhiyun 			memcpy(aligned_buf, buf, len);
2347*4882a593Smuzhiyun 			buf += len;
2348*4882a593Smuzhiyun 			cnt -= len;
2349*4882a593Smuzhiyun 			/* push data from aligned buffer into fifo */
2350*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2351*4882a593Smuzhiyun 				mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
2352*4882a593Smuzhiyun 		}
2353*4882a593Smuzhiyun 	} else
2354*4882a593Smuzhiyun #endif
2355*4882a593Smuzhiyun 	{
2356*4882a593Smuzhiyun 		u16 *pdata = buf;
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 		for (; cnt >= 2; cnt -= 2)
2359*4882a593Smuzhiyun 			mci_fifo_writew(host->fifo_reg, *pdata++);
2360*4882a593Smuzhiyun 		buf = pdata;
2361*4882a593Smuzhiyun 	}
2362*4882a593Smuzhiyun 	/* put anything remaining in the part_buf */
2363*4882a593Smuzhiyun 	if (cnt) {
2364*4882a593Smuzhiyun 		dw_mci_set_part_bytes(host, buf, cnt);
2365*4882a593Smuzhiyun 		 /* Push data if we have reached the expected data length */
2366*4882a593Smuzhiyun 		if ((data->bytes_xfered + init_cnt) ==
2367*4882a593Smuzhiyun 		    (data->blksz * data->blocks))
2368*4882a593Smuzhiyun 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
2369*4882a593Smuzhiyun 	}
2370*4882a593Smuzhiyun }
2371*4882a593Smuzhiyun 
dw_mci_pull_data16(struct dw_mci * host,void * buf,int cnt)2372*4882a593Smuzhiyun static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2373*4882a593Smuzhiyun {
2374*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2375*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x1)) {
2376*4882a593Smuzhiyun 		while (cnt >= 2) {
2377*4882a593Smuzhiyun 			/* pull data from fifo into aligned buffer */
2378*4882a593Smuzhiyun 			u16 aligned_buf[64];
2379*4882a593Smuzhiyun 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
2380*4882a593Smuzhiyun 			int items = len >> 1;
2381*4882a593Smuzhiyun 			int i;
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2384*4882a593Smuzhiyun 				aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
2385*4882a593Smuzhiyun 			/* memcpy from aligned buffer into output buffer */
2386*4882a593Smuzhiyun 			memcpy(buf, aligned_buf, len);
2387*4882a593Smuzhiyun 			buf += len;
2388*4882a593Smuzhiyun 			cnt -= len;
2389*4882a593Smuzhiyun 		}
2390*4882a593Smuzhiyun 	} else
2391*4882a593Smuzhiyun #endif
2392*4882a593Smuzhiyun 	{
2393*4882a593Smuzhiyun 		u16 *pdata = buf;
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 		for (; cnt >= 2; cnt -= 2)
2396*4882a593Smuzhiyun 			*pdata++ = mci_fifo_readw(host->fifo_reg);
2397*4882a593Smuzhiyun 		buf = pdata;
2398*4882a593Smuzhiyun 	}
2399*4882a593Smuzhiyun 	if (cnt) {
2400*4882a593Smuzhiyun 		host->part_buf16 = mci_fifo_readw(host->fifo_reg);
2401*4882a593Smuzhiyun 		dw_mci_pull_final_bytes(host, buf, cnt);
2402*4882a593Smuzhiyun 	}
2403*4882a593Smuzhiyun }
2404*4882a593Smuzhiyun 
dw_mci_push_data32(struct dw_mci * host,void * buf,int cnt)2405*4882a593Smuzhiyun static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2406*4882a593Smuzhiyun {
2407*4882a593Smuzhiyun 	struct mmc_data *data = host->data;
2408*4882a593Smuzhiyun 	int init_cnt = cnt;
2409*4882a593Smuzhiyun 
2410*4882a593Smuzhiyun 	/* try and push anything in the part_buf */
2411*4882a593Smuzhiyun 	if (unlikely(host->part_buf_count)) {
2412*4882a593Smuzhiyun 		int len = dw_mci_push_part_bytes(host, buf, cnt);
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun 		buf += len;
2415*4882a593Smuzhiyun 		cnt -= len;
2416*4882a593Smuzhiyun 		if (host->part_buf_count == 4) {
2417*4882a593Smuzhiyun 			mci_fifo_writel(host->fifo_reg,	host->part_buf32);
2418*4882a593Smuzhiyun 			host->part_buf_count = 0;
2419*4882a593Smuzhiyun 		}
2420*4882a593Smuzhiyun 	}
2421*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2422*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x3)) {
2423*4882a593Smuzhiyun 		while (cnt >= 4) {
2424*4882a593Smuzhiyun 			u32 aligned_buf[32];
2425*4882a593Smuzhiyun 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
2426*4882a593Smuzhiyun 			int items = len >> 2;
2427*4882a593Smuzhiyun 			int i;
2428*4882a593Smuzhiyun 			/* memcpy from input buffer into aligned buffer */
2429*4882a593Smuzhiyun 			memcpy(aligned_buf, buf, len);
2430*4882a593Smuzhiyun 			buf += len;
2431*4882a593Smuzhiyun 			cnt -= len;
2432*4882a593Smuzhiyun 			/* push data from aligned buffer into fifo */
2433*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2434*4882a593Smuzhiyun 				mci_fifo_writel(host->fifo_reg,	aligned_buf[i]);
2435*4882a593Smuzhiyun 		}
2436*4882a593Smuzhiyun 	} else
2437*4882a593Smuzhiyun #endif
2438*4882a593Smuzhiyun 	{
2439*4882a593Smuzhiyun 		u32 *pdata = buf;
2440*4882a593Smuzhiyun 
2441*4882a593Smuzhiyun 		for (; cnt >= 4; cnt -= 4)
2442*4882a593Smuzhiyun 			mci_fifo_writel(host->fifo_reg, *pdata++);
2443*4882a593Smuzhiyun 		buf = pdata;
2444*4882a593Smuzhiyun 	}
2445*4882a593Smuzhiyun 	/* put anything remaining in the part_buf */
2446*4882a593Smuzhiyun 	if (cnt) {
2447*4882a593Smuzhiyun 		dw_mci_set_part_bytes(host, buf, cnt);
2448*4882a593Smuzhiyun 		 /* Push data if we have reached the expected data length */
2449*4882a593Smuzhiyun 		if ((data->bytes_xfered + init_cnt) ==
2450*4882a593Smuzhiyun 		    (data->blksz * data->blocks))
2451*4882a593Smuzhiyun 			mci_fifo_writel(host->fifo_reg, host->part_buf32);
2452*4882a593Smuzhiyun 	}
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun 
dw_mci_pull_data32(struct dw_mci * host,void * buf,int cnt)2455*4882a593Smuzhiyun static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2456*4882a593Smuzhiyun {
2457*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2458*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x3)) {
2459*4882a593Smuzhiyun 		while (cnt >= 4) {
2460*4882a593Smuzhiyun 			/* pull data from fifo into aligned buffer */
2461*4882a593Smuzhiyun 			u32 aligned_buf[32];
2462*4882a593Smuzhiyun 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
2463*4882a593Smuzhiyun 			int items = len >> 2;
2464*4882a593Smuzhiyun 			int i;
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2467*4882a593Smuzhiyun 				aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
2468*4882a593Smuzhiyun 			/* memcpy from aligned buffer into output buffer */
2469*4882a593Smuzhiyun 			memcpy(buf, aligned_buf, len);
2470*4882a593Smuzhiyun 			buf += len;
2471*4882a593Smuzhiyun 			cnt -= len;
2472*4882a593Smuzhiyun 		}
2473*4882a593Smuzhiyun 	} else
2474*4882a593Smuzhiyun #endif
2475*4882a593Smuzhiyun 	{
2476*4882a593Smuzhiyun 		u32 *pdata = buf;
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun 		for (; cnt >= 4; cnt -= 4)
2479*4882a593Smuzhiyun 			*pdata++ = mci_fifo_readl(host->fifo_reg);
2480*4882a593Smuzhiyun 		buf = pdata;
2481*4882a593Smuzhiyun 	}
2482*4882a593Smuzhiyun 	if (cnt) {
2483*4882a593Smuzhiyun 		host->part_buf32 = mci_fifo_readl(host->fifo_reg);
2484*4882a593Smuzhiyun 		dw_mci_pull_final_bytes(host, buf, cnt);
2485*4882a593Smuzhiyun 	}
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun 
dw_mci_push_data64(struct dw_mci * host,void * buf,int cnt)2488*4882a593Smuzhiyun static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2489*4882a593Smuzhiyun {
2490*4882a593Smuzhiyun 	struct mmc_data *data = host->data;
2491*4882a593Smuzhiyun 	int init_cnt = cnt;
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun 	/* try and push anything in the part_buf */
2494*4882a593Smuzhiyun 	if (unlikely(host->part_buf_count)) {
2495*4882a593Smuzhiyun 		int len = dw_mci_push_part_bytes(host, buf, cnt);
2496*4882a593Smuzhiyun 
2497*4882a593Smuzhiyun 		buf += len;
2498*4882a593Smuzhiyun 		cnt -= len;
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 		if (host->part_buf_count == 8) {
2501*4882a593Smuzhiyun 			mci_fifo_writeq(host->fifo_reg,	host->part_buf);
2502*4882a593Smuzhiyun 			host->part_buf_count = 0;
2503*4882a593Smuzhiyun 		}
2504*4882a593Smuzhiyun 	}
2505*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2506*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x7)) {
2507*4882a593Smuzhiyun 		while (cnt >= 8) {
2508*4882a593Smuzhiyun 			u64 aligned_buf[16];
2509*4882a593Smuzhiyun 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
2510*4882a593Smuzhiyun 			int items = len >> 3;
2511*4882a593Smuzhiyun 			int i;
2512*4882a593Smuzhiyun 			/* memcpy from input buffer into aligned buffer */
2513*4882a593Smuzhiyun 			memcpy(aligned_buf, buf, len);
2514*4882a593Smuzhiyun 			buf += len;
2515*4882a593Smuzhiyun 			cnt -= len;
2516*4882a593Smuzhiyun 			/* push data from aligned buffer into fifo */
2517*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2518*4882a593Smuzhiyun 				mci_fifo_writeq(host->fifo_reg,	aligned_buf[i]);
2519*4882a593Smuzhiyun 		}
2520*4882a593Smuzhiyun 	} else
2521*4882a593Smuzhiyun #endif
2522*4882a593Smuzhiyun 	{
2523*4882a593Smuzhiyun 		u64 *pdata = buf;
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun 		for (; cnt >= 8; cnt -= 8)
2526*4882a593Smuzhiyun 			mci_fifo_writeq(host->fifo_reg, *pdata++);
2527*4882a593Smuzhiyun 		buf = pdata;
2528*4882a593Smuzhiyun 	}
2529*4882a593Smuzhiyun 	/* put anything remaining in the part_buf */
2530*4882a593Smuzhiyun 	if (cnt) {
2531*4882a593Smuzhiyun 		dw_mci_set_part_bytes(host, buf, cnt);
2532*4882a593Smuzhiyun 		/* Push data if we have reached the expected data length */
2533*4882a593Smuzhiyun 		if ((data->bytes_xfered + init_cnt) ==
2534*4882a593Smuzhiyun 		    (data->blksz * data->blocks))
2535*4882a593Smuzhiyun 			mci_fifo_writeq(host->fifo_reg, host->part_buf);
2536*4882a593Smuzhiyun 	}
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun 
dw_mci_pull_data64(struct dw_mci * host,void * buf,int cnt)2539*4882a593Smuzhiyun static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2540*4882a593Smuzhiyun {
2541*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2542*4882a593Smuzhiyun 	if (unlikely((unsigned long)buf & 0x7)) {
2543*4882a593Smuzhiyun 		while (cnt >= 8) {
2544*4882a593Smuzhiyun 			/* pull data from fifo into aligned buffer */
2545*4882a593Smuzhiyun 			u64 aligned_buf[16];
2546*4882a593Smuzhiyun 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
2547*4882a593Smuzhiyun 			int items = len >> 3;
2548*4882a593Smuzhiyun 			int i;
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 			for (i = 0; i < items; ++i)
2551*4882a593Smuzhiyun 				aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 			/* memcpy from aligned buffer into output buffer */
2554*4882a593Smuzhiyun 			memcpy(buf, aligned_buf, len);
2555*4882a593Smuzhiyun 			buf += len;
2556*4882a593Smuzhiyun 			cnt -= len;
2557*4882a593Smuzhiyun 		}
2558*4882a593Smuzhiyun 	} else
2559*4882a593Smuzhiyun #endif
2560*4882a593Smuzhiyun 	{
2561*4882a593Smuzhiyun 		u64 *pdata = buf;
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun 		for (; cnt >= 8; cnt -= 8)
2564*4882a593Smuzhiyun 			*pdata++ = mci_fifo_readq(host->fifo_reg);
2565*4882a593Smuzhiyun 		buf = pdata;
2566*4882a593Smuzhiyun 	}
2567*4882a593Smuzhiyun 	if (cnt) {
2568*4882a593Smuzhiyun 		host->part_buf = mci_fifo_readq(host->fifo_reg);
2569*4882a593Smuzhiyun 		dw_mci_pull_final_bytes(host, buf, cnt);
2570*4882a593Smuzhiyun 	}
2571*4882a593Smuzhiyun }
2572*4882a593Smuzhiyun 
dw_mci_pull_data(struct dw_mci * host,void * buf,int cnt)2573*4882a593Smuzhiyun static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2574*4882a593Smuzhiyun {
2575*4882a593Smuzhiyun 	int len;
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun 	/* get remaining partial bytes */
2578*4882a593Smuzhiyun 	len = dw_mci_pull_part_bytes(host, buf, cnt);
2579*4882a593Smuzhiyun 	if (unlikely(len == cnt))
2580*4882a593Smuzhiyun 		return;
2581*4882a593Smuzhiyun 	buf += len;
2582*4882a593Smuzhiyun 	cnt -= len;
2583*4882a593Smuzhiyun 
2584*4882a593Smuzhiyun 	/* get the rest of the data */
2585*4882a593Smuzhiyun 	host->pull_data(host, buf, cnt);
2586*4882a593Smuzhiyun }
2587*4882a593Smuzhiyun 
dw_mci_read_data_pio(struct dw_mci * host,bool dto)2588*4882a593Smuzhiyun static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2589*4882a593Smuzhiyun {
2590*4882a593Smuzhiyun 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2591*4882a593Smuzhiyun 	void *buf;
2592*4882a593Smuzhiyun 	unsigned int offset;
2593*4882a593Smuzhiyun 	struct mmc_data	*data = host->data;
2594*4882a593Smuzhiyun 	int shift = host->data_shift;
2595*4882a593Smuzhiyun 	u32 status;
2596*4882a593Smuzhiyun 	unsigned int len;
2597*4882a593Smuzhiyun 	unsigned int remain, fcnt;
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 	do {
2600*4882a593Smuzhiyun 		if (!sg_miter_next(sg_miter))
2601*4882a593Smuzhiyun 			goto done;
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun 		host->sg = sg_miter->piter.sg;
2604*4882a593Smuzhiyun 		buf = sg_miter->addr;
2605*4882a593Smuzhiyun 		remain = sg_miter->length;
2606*4882a593Smuzhiyun 		offset = 0;
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 		do {
2609*4882a593Smuzhiyun 			fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2610*4882a593Smuzhiyun 					<< shift) + host->part_buf_count;
2611*4882a593Smuzhiyun 			len = min(remain, fcnt);
2612*4882a593Smuzhiyun 			if (!len)
2613*4882a593Smuzhiyun 				break;
2614*4882a593Smuzhiyun 			dw_mci_pull_data(host, (void *)(buf + offset), len);
2615*4882a593Smuzhiyun 			data->bytes_xfered += len;
2616*4882a593Smuzhiyun 			offset += len;
2617*4882a593Smuzhiyun 			remain -= len;
2618*4882a593Smuzhiyun 		} while (remain);
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 		sg_miter->consumed = offset;
2621*4882a593Smuzhiyun 		status = mci_readl(host, MINTSTS);
2622*4882a593Smuzhiyun 		mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2623*4882a593Smuzhiyun 	/* if the RXDR is ready read again */
2624*4882a593Smuzhiyun 	} while ((status & SDMMC_INT_RXDR) ||
2625*4882a593Smuzhiyun 		 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 	if (!remain) {
2628*4882a593Smuzhiyun 		if (!sg_miter_next(sg_miter))
2629*4882a593Smuzhiyun 			goto done;
2630*4882a593Smuzhiyun 		sg_miter->consumed = 0;
2631*4882a593Smuzhiyun 	}
2632*4882a593Smuzhiyun 	sg_miter_stop(sg_miter);
2633*4882a593Smuzhiyun 	return;
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun done:
2636*4882a593Smuzhiyun 	sg_miter_stop(sg_miter);
2637*4882a593Smuzhiyun 	host->sg = NULL;
2638*4882a593Smuzhiyun 	smp_wmb(); /* drain writebuffer */
2639*4882a593Smuzhiyun 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2640*4882a593Smuzhiyun 	if (host->need_xfer_timer)
2641*4882a593Smuzhiyun 		del_timer(&host->xfer_timer);
2642*4882a593Smuzhiyun }
2643*4882a593Smuzhiyun 
dw_mci_write_data_pio(struct dw_mci * host)2644*4882a593Smuzhiyun static void dw_mci_write_data_pio(struct dw_mci *host)
2645*4882a593Smuzhiyun {
2646*4882a593Smuzhiyun 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2647*4882a593Smuzhiyun 	void *buf;
2648*4882a593Smuzhiyun 	unsigned int offset;
2649*4882a593Smuzhiyun 	struct mmc_data	*data = host->data;
2650*4882a593Smuzhiyun 	int shift = host->data_shift;
2651*4882a593Smuzhiyun 	u32 status;
2652*4882a593Smuzhiyun 	unsigned int len;
2653*4882a593Smuzhiyun 	unsigned int fifo_depth = host->fifo_depth;
2654*4882a593Smuzhiyun 	unsigned int remain, fcnt;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	do {
2657*4882a593Smuzhiyun 		if (!sg_miter_next(sg_miter))
2658*4882a593Smuzhiyun 			goto done;
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun 		host->sg = sg_miter->piter.sg;
2661*4882a593Smuzhiyun 		buf = sg_miter->addr;
2662*4882a593Smuzhiyun 		remain = sg_miter->length;
2663*4882a593Smuzhiyun 		offset = 0;
2664*4882a593Smuzhiyun 
2665*4882a593Smuzhiyun 		do {
2666*4882a593Smuzhiyun 			fcnt = ((fifo_depth -
2667*4882a593Smuzhiyun 				 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2668*4882a593Smuzhiyun 					<< shift) - host->part_buf_count;
2669*4882a593Smuzhiyun 			len = min(remain, fcnt);
2670*4882a593Smuzhiyun 			if (!len)
2671*4882a593Smuzhiyun 				break;
2672*4882a593Smuzhiyun 			host->push_data(host, (void *)(buf + offset), len);
2673*4882a593Smuzhiyun 			data->bytes_xfered += len;
2674*4882a593Smuzhiyun 			offset += len;
2675*4882a593Smuzhiyun 			remain -= len;
2676*4882a593Smuzhiyun 		} while (remain);
2677*4882a593Smuzhiyun 
2678*4882a593Smuzhiyun 		sg_miter->consumed = offset;
2679*4882a593Smuzhiyun 		status = mci_readl(host, MINTSTS);
2680*4882a593Smuzhiyun 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2681*4882a593Smuzhiyun 	} while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 	if (!remain) {
2684*4882a593Smuzhiyun 		if (!sg_miter_next(sg_miter))
2685*4882a593Smuzhiyun 			goto done;
2686*4882a593Smuzhiyun 		sg_miter->consumed = 0;
2687*4882a593Smuzhiyun 	}
2688*4882a593Smuzhiyun 	sg_miter_stop(sg_miter);
2689*4882a593Smuzhiyun 	return;
2690*4882a593Smuzhiyun 
2691*4882a593Smuzhiyun done:
2692*4882a593Smuzhiyun 	sg_miter_stop(sg_miter);
2693*4882a593Smuzhiyun 	host->sg = NULL;
2694*4882a593Smuzhiyun 	smp_wmb(); /* drain writebuffer */
2695*4882a593Smuzhiyun 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2696*4882a593Smuzhiyun }
2697*4882a593Smuzhiyun 
dw_mci_cmd_interrupt(struct dw_mci * host,u32 status)2698*4882a593Smuzhiyun static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2699*4882a593Smuzhiyun {
2700*4882a593Smuzhiyun 	del_timer(&host->cto_timer);
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun 	if (!host->cmd_status)
2703*4882a593Smuzhiyun 		host->cmd_status = status;
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun 	smp_wmb(); /* drain writebuffer */
2706*4882a593Smuzhiyun 
2707*4882a593Smuzhiyun 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2708*4882a593Smuzhiyun 	tasklet_schedule(&host->tasklet);
2709*4882a593Smuzhiyun }
2710*4882a593Smuzhiyun 
dw_mci_handle_cd(struct dw_mci * host)2711*4882a593Smuzhiyun static void dw_mci_handle_cd(struct dw_mci *host)
2712*4882a593Smuzhiyun {
2713*4882a593Smuzhiyun 	struct dw_mci_slot *slot = host->slot;
2714*4882a593Smuzhiyun 
2715*4882a593Smuzhiyun 	if (slot->mmc->ops->card_event)
2716*4882a593Smuzhiyun 		slot->mmc->ops->card_event(slot->mmc);
2717*4882a593Smuzhiyun 	mmc_detect_change(slot->mmc,
2718*4882a593Smuzhiyun 		msecs_to_jiffies(host->pdata->detect_delay_ms));
2719*4882a593Smuzhiyun }
2720*4882a593Smuzhiyun 
dw_mci_interrupt(int irq,void * dev_id)2721*4882a593Smuzhiyun static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2722*4882a593Smuzhiyun {
2723*4882a593Smuzhiyun 	struct dw_mci *host = dev_id;
2724*4882a593Smuzhiyun 	u32 pending;
2725*4882a593Smuzhiyun 	struct dw_mci_slot *slot = host->slot;
2726*4882a593Smuzhiyun 	unsigned long irqflags;
2727*4882a593Smuzhiyun 
2728*4882a593Smuzhiyun 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun 	if (pending) {
2731*4882a593Smuzhiyun 		/* Check volt switch first, since it can look like an error */
2732*4882a593Smuzhiyun 		if ((host->state == STATE_SENDING_CMD11) &&
2733*4882a593Smuzhiyun 		    (pending & SDMMC_INT_VOLT_SWITCH)) {
2734*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2735*4882a593Smuzhiyun 			pending &= ~SDMMC_INT_VOLT_SWITCH;
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun 			/*
2738*4882a593Smuzhiyun 			 * Hold the lock; we know cmd11_timer can't be kicked
2739*4882a593Smuzhiyun 			 * off after the lock is released, so safe to delete.
2740*4882a593Smuzhiyun 			 */
2741*4882a593Smuzhiyun 			spin_lock_irqsave(&host->irq_lock, irqflags);
2742*4882a593Smuzhiyun 			dw_mci_cmd_interrupt(host, pending);
2743*4882a593Smuzhiyun 			spin_unlock_irqrestore(&host->irq_lock, irqflags);
2744*4882a593Smuzhiyun 
2745*4882a593Smuzhiyun 			del_timer(&host->cmd11_timer);
2746*4882a593Smuzhiyun 		}
2747*4882a593Smuzhiyun 
2748*4882a593Smuzhiyun 		if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2749*4882a593Smuzhiyun 			spin_lock_irqsave(&host->irq_lock, irqflags);
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun 			del_timer(&host->cto_timer);
2752*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2753*4882a593Smuzhiyun 			host->cmd_status = pending;
2754*4882a593Smuzhiyun 			if ((host->need_xfer_timer) &&
2755*4882a593Smuzhiyun 			     host->dir_status == DW_MCI_RECV_STATUS)
2756*4882a593Smuzhiyun 				del_timer(&host->xfer_timer);
2757*4882a593Smuzhiyun 			smp_wmb(); /* drain writebuffer */
2758*4882a593Smuzhiyun 			set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 			spin_unlock_irqrestore(&host->irq_lock, irqflags);
2761*4882a593Smuzhiyun 		}
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun 		if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2764*4882a593Smuzhiyun 			/* if there is an error report DATA_ERROR */
2765*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2766*4882a593Smuzhiyun 			host->data_status = pending;
2767*4882a593Smuzhiyun 			smp_wmb(); /* drain writebuffer */
2768*4882a593Smuzhiyun 			set_bit(EVENT_DATA_ERROR, &host->pending_events);
2769*4882a593Smuzhiyun 			tasklet_schedule(&host->tasklet);
2770*4882a593Smuzhiyun 		}
2771*4882a593Smuzhiyun 
2772*4882a593Smuzhiyun 		if (pending & SDMMC_INT_DATA_OVER) {
2773*4882a593Smuzhiyun rv1106_sd:
2774*4882a593Smuzhiyun 			spin_lock_irqsave(&host->irq_lock, irqflags);
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 			del_timer(&host->dto_timer);
2777*4882a593Smuzhiyun 
2778*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2779*4882a593Smuzhiyun 			if (host->is_rv1106_sd)
2780*4882a593Smuzhiyun 				pending |= SDMMC_INT_DATA_OVER;
2781*4882a593Smuzhiyun 			if (!host->data_status)
2782*4882a593Smuzhiyun 				host->data_status = pending;
2783*4882a593Smuzhiyun 			smp_wmb(); /* drain writebuffer */
2784*4882a593Smuzhiyun 			if (host->dir_status == DW_MCI_RECV_STATUS) {
2785*4882a593Smuzhiyun 				if (host->sg != NULL)
2786*4882a593Smuzhiyun 					dw_mci_read_data_pio(host, true);
2787*4882a593Smuzhiyun 			}
2788*4882a593Smuzhiyun 			set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2789*4882a593Smuzhiyun 			tasklet_schedule(&host->tasklet);
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun 			spin_unlock_irqrestore(&host->irq_lock, irqflags);
2792*4882a593Smuzhiyun 		}
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun 		if (pending & SDMMC_INT_RXDR) {
2795*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2796*4882a593Smuzhiyun 			if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2797*4882a593Smuzhiyun 				dw_mci_read_data_pio(host, false);
2798*4882a593Smuzhiyun 		}
2799*4882a593Smuzhiyun 
2800*4882a593Smuzhiyun 		if (pending & SDMMC_INT_TXDR) {
2801*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2802*4882a593Smuzhiyun 			if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2803*4882a593Smuzhiyun 				dw_mci_write_data_pio(host);
2804*4882a593Smuzhiyun 		}
2805*4882a593Smuzhiyun 
2806*4882a593Smuzhiyun 		if (pending & SDMMC_INT_CMD_DONE) {
2807*4882a593Smuzhiyun 			spin_lock_irqsave(&host->irq_lock, irqflags);
2808*4882a593Smuzhiyun 
2809*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2810*4882a593Smuzhiyun 			dw_mci_cmd_interrupt(host, pending);
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun 			spin_unlock_irqrestore(&host->irq_lock, irqflags);
2813*4882a593Smuzhiyun 		}
2814*4882a593Smuzhiyun 
2815*4882a593Smuzhiyun 		if (pending & SDMMC_INT_CD) {
2816*4882a593Smuzhiyun 			mci_writel(host, RINTSTS, SDMMC_INT_CD);
2817*4882a593Smuzhiyun 			dw_mci_handle_cd(host);
2818*4882a593Smuzhiyun 		}
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun 		if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2821*4882a593Smuzhiyun 			mci_writel(host, RINTSTS,
2822*4882a593Smuzhiyun 				   SDMMC_INT_SDIO(slot->sdio_id));
2823*4882a593Smuzhiyun 			__dw_mci_enable_sdio_irq(slot, 0);
2824*4882a593Smuzhiyun 			sdio_signal_irq(slot->mmc);
2825*4882a593Smuzhiyun 		}
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun 	}
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun 	if (host->use_dma != TRANS_MODE_IDMAC)
2830*4882a593Smuzhiyun 		return IRQ_HANDLED;
2831*4882a593Smuzhiyun 
2832*4882a593Smuzhiyun 	/* Handle IDMA interrupts */
2833*4882a593Smuzhiyun 	if (host->dma_64bit_address == 1) {
2834*4882a593Smuzhiyun 		pending = mci_readl(host, IDSTS64);
2835*4882a593Smuzhiyun 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2836*4882a593Smuzhiyun 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2837*4882a593Smuzhiyun 							SDMMC_IDMAC_INT_RI);
2838*4882a593Smuzhiyun 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2839*4882a593Smuzhiyun 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2840*4882a593Smuzhiyun 				host->dma_ops->complete((void *)host);
2841*4882a593Smuzhiyun 		}
2842*4882a593Smuzhiyun 	} else {
2843*4882a593Smuzhiyun 		pending = mci_readl(host, IDSTS);
2844*4882a593Smuzhiyun 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2845*4882a593Smuzhiyun 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2846*4882a593Smuzhiyun 							SDMMC_IDMAC_INT_RI);
2847*4882a593Smuzhiyun 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2848*4882a593Smuzhiyun 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2849*4882a593Smuzhiyun 				host->dma_ops->complete((void *)host);
2850*4882a593Smuzhiyun 
2851*4882a593Smuzhiyun 			if (host->is_rv1106_sd && (pending & SDMMC_IDMAC_INT_TI))
2852*4882a593Smuzhiyun 				goto rv1106_sd;
2853*4882a593Smuzhiyun 		}
2854*4882a593Smuzhiyun 	}
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun 	return IRQ_HANDLED;
2857*4882a593Smuzhiyun }
2858*4882a593Smuzhiyun 
dw_mci_init_slot_caps(struct dw_mci_slot * slot)2859*4882a593Smuzhiyun static int dw_mci_init_slot_caps(struct dw_mci_slot *slot)
2860*4882a593Smuzhiyun {
2861*4882a593Smuzhiyun 	struct dw_mci *host = slot->host;
2862*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
2863*4882a593Smuzhiyun 	struct mmc_host *mmc = slot->mmc;
2864*4882a593Smuzhiyun 	int ctrl_id;
2865*4882a593Smuzhiyun 
2866*4882a593Smuzhiyun 	if (host->pdata->caps)
2867*4882a593Smuzhiyun 		mmc->caps = host->pdata->caps;
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun 	if (host->pdata->pm_caps)
2870*4882a593Smuzhiyun 		mmc->pm_caps = host->pdata->pm_caps;
2871*4882a593Smuzhiyun 
2872*4882a593Smuzhiyun 	if (host->dev->of_node) {
2873*4882a593Smuzhiyun 		ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2874*4882a593Smuzhiyun 		if (ctrl_id < 0)
2875*4882a593Smuzhiyun 			ctrl_id = 0;
2876*4882a593Smuzhiyun 	} else {
2877*4882a593Smuzhiyun 		ctrl_id = to_platform_device(host->dev)->id;
2878*4882a593Smuzhiyun 	}
2879*4882a593Smuzhiyun 
2880*4882a593Smuzhiyun 	if (drv_data && drv_data->caps) {
2881*4882a593Smuzhiyun 		if (ctrl_id >= drv_data->num_caps) {
2882*4882a593Smuzhiyun 			dev_err(host->dev, "invalid controller id %d\n",
2883*4882a593Smuzhiyun 				ctrl_id);
2884*4882a593Smuzhiyun 			return -EINVAL;
2885*4882a593Smuzhiyun 		}
2886*4882a593Smuzhiyun 		mmc->caps |= drv_data->caps[ctrl_id];
2887*4882a593Smuzhiyun 	}
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	if (host->pdata->caps2)
2890*4882a593Smuzhiyun 		mmc->caps2 = host->pdata->caps2;
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	mmc->f_min = DW_MCI_FREQ_MIN;
2893*4882a593Smuzhiyun 	if (!mmc->f_max)
2894*4882a593Smuzhiyun 		mmc->f_max = DW_MCI_FREQ_MAX;
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	/* Process SDIO IRQs through the sdio_irq_work. */
2897*4882a593Smuzhiyun 	if (mmc->caps & MMC_CAP_SDIO_IRQ)
2898*4882a593Smuzhiyun 		mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 	return 0;
2901*4882a593Smuzhiyun }
2902*4882a593Smuzhiyun 
dw_mci_init_slot(struct dw_mci * host)2903*4882a593Smuzhiyun static int dw_mci_init_slot(struct dw_mci *host)
2904*4882a593Smuzhiyun {
2905*4882a593Smuzhiyun 	struct mmc_host *mmc;
2906*4882a593Smuzhiyun 	struct dw_mci_slot *slot;
2907*4882a593Smuzhiyun 	int ret;
2908*4882a593Smuzhiyun 
2909*4882a593Smuzhiyun 	mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2910*4882a593Smuzhiyun 	if (!mmc)
2911*4882a593Smuzhiyun 		return -ENOMEM;
2912*4882a593Smuzhiyun 
2913*4882a593Smuzhiyun 	slot = mmc_priv(mmc);
2914*4882a593Smuzhiyun 	slot->id = 0;
2915*4882a593Smuzhiyun 	slot->sdio_id = host->sdio_id0 + slot->id;
2916*4882a593Smuzhiyun 	slot->mmc = mmc;
2917*4882a593Smuzhiyun 	slot->host = host;
2918*4882a593Smuzhiyun 	host->slot = slot;
2919*4882a593Smuzhiyun 
2920*4882a593Smuzhiyun 	mmc->ops = &dw_mci_ops;
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun 	/*if there are external regulators, get them*/
2923*4882a593Smuzhiyun 	ret = mmc_regulator_get_supply(mmc);
2924*4882a593Smuzhiyun 	if (ret)
2925*4882a593Smuzhiyun 		goto err_host_allocated;
2926*4882a593Smuzhiyun 
2927*4882a593Smuzhiyun 	if (!mmc->ocr_avail)
2928*4882a593Smuzhiyun 		mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	ret = mmc_of_parse(mmc);
2931*4882a593Smuzhiyun 	if (ret)
2932*4882a593Smuzhiyun 		goto err_host_allocated;
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 	ret = dw_mci_init_slot_caps(slot);
2935*4882a593Smuzhiyun 	if (ret)
2936*4882a593Smuzhiyun 		goto err_host_allocated;
2937*4882a593Smuzhiyun 
2938*4882a593Smuzhiyun 	/* Useful defaults if platform data is unset. */
2939*4882a593Smuzhiyun 	if (host->use_dma == TRANS_MODE_IDMAC) {
2940*4882a593Smuzhiyun 		/* Reserve last desc for dirty data */
2941*4882a593Smuzhiyun 		if (host->is_rv1106_sd)
2942*4882a593Smuzhiyun 			host->ring_size--;
2943*4882a593Smuzhiyun 
2944*4882a593Smuzhiyun 		mmc->max_segs = host->ring_size;
2945*4882a593Smuzhiyun 		mmc->max_blk_size = 65535;
2946*4882a593Smuzhiyun 		mmc->max_seg_size = 0x1000;
2947*4882a593Smuzhiyun 		mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2948*4882a593Smuzhiyun 		mmc->max_blk_count = mmc->max_req_size / 512;
2949*4882a593Smuzhiyun 	} else if (host->use_dma == TRANS_MODE_EDMAC) {
2950*4882a593Smuzhiyun 		mmc->max_segs = 64;
2951*4882a593Smuzhiyun 		mmc->max_blk_size = 65535;
2952*4882a593Smuzhiyun 		mmc->max_blk_count = 65535;
2953*4882a593Smuzhiyun 		mmc->max_req_size =
2954*4882a593Smuzhiyun 				mmc->max_blk_size * mmc->max_blk_count;
2955*4882a593Smuzhiyun 		mmc->max_seg_size = mmc->max_req_size;
2956*4882a593Smuzhiyun 	} else {
2957*4882a593Smuzhiyun 		/* TRANS_MODE_PIO */
2958*4882a593Smuzhiyun 		mmc->max_segs = 64;
2959*4882a593Smuzhiyun 		mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
2960*4882a593Smuzhiyun 		mmc->max_blk_count = 512;
2961*4882a593Smuzhiyun 		mmc->max_req_size = mmc->max_blk_size *
2962*4882a593Smuzhiyun 				    mmc->max_blk_count;
2963*4882a593Smuzhiyun 		mmc->max_seg_size = mmc->max_req_size;
2964*4882a593Smuzhiyun 	}
2965*4882a593Smuzhiyun 
2966*4882a593Smuzhiyun 	dw_mci_get_cd(mmc);
2967*4882a593Smuzhiyun 
2968*4882a593Smuzhiyun 	ret = mmc_add_host(mmc);
2969*4882a593Smuzhiyun 	if (ret)
2970*4882a593Smuzhiyun 		goto err_host_allocated;
2971*4882a593Smuzhiyun 
2972*4882a593Smuzhiyun #if defined(CONFIG_DEBUG_FS)
2973*4882a593Smuzhiyun 	dw_mci_init_debugfs(slot);
2974*4882a593Smuzhiyun #endif
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun 	return 0;
2977*4882a593Smuzhiyun 
2978*4882a593Smuzhiyun err_host_allocated:
2979*4882a593Smuzhiyun 	mmc_free_host(mmc);
2980*4882a593Smuzhiyun 	return ret;
2981*4882a593Smuzhiyun }
2982*4882a593Smuzhiyun 
dw_mci_cleanup_slot(struct dw_mci_slot * slot)2983*4882a593Smuzhiyun static void dw_mci_cleanup_slot(struct dw_mci_slot *slot)
2984*4882a593Smuzhiyun {
2985*4882a593Smuzhiyun 	/* Debugfs stuff is cleaned up by mmc core */
2986*4882a593Smuzhiyun 	mmc_remove_host(slot->mmc);
2987*4882a593Smuzhiyun 	slot->host->slot = NULL;
2988*4882a593Smuzhiyun 	mmc_free_host(slot->mmc);
2989*4882a593Smuzhiyun }
2990*4882a593Smuzhiyun 
dw_mci_init_dma(struct dw_mci * host)2991*4882a593Smuzhiyun static void dw_mci_init_dma(struct dw_mci *host)
2992*4882a593Smuzhiyun {
2993*4882a593Smuzhiyun 	int addr_config;
2994*4882a593Smuzhiyun 	struct device *dev = host->dev;
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun 	/*
2997*4882a593Smuzhiyun 	* Check tansfer mode from HCON[17:16]
2998*4882a593Smuzhiyun 	* Clear the ambiguous description of dw_mmc databook:
2999*4882a593Smuzhiyun 	* 2b'00: No DMA Interface -> Actually means using Internal DMA block
3000*4882a593Smuzhiyun 	* 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
3001*4882a593Smuzhiyun 	* 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
3002*4882a593Smuzhiyun 	* 2b'11: Non DW DMA Interface -> pio only
3003*4882a593Smuzhiyun 	* Compared to DesignWare DMA Interface, Generic DMA Interface has a
3004*4882a593Smuzhiyun 	* simpler request/acknowledge handshake mechanism and both of them
3005*4882a593Smuzhiyun 	* are regarded as external dma master for dw_mmc.
3006*4882a593Smuzhiyun 	*/
3007*4882a593Smuzhiyun 	host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
3008*4882a593Smuzhiyun 	if (host->use_dma == DMA_INTERFACE_IDMA) {
3009*4882a593Smuzhiyun 		host->use_dma = TRANS_MODE_IDMAC;
3010*4882a593Smuzhiyun 	} else if (host->use_dma == DMA_INTERFACE_DWDMA ||
3011*4882a593Smuzhiyun 		   host->use_dma == DMA_INTERFACE_GDMA) {
3012*4882a593Smuzhiyun 		host->use_dma = TRANS_MODE_EDMAC;
3013*4882a593Smuzhiyun 	} else {
3014*4882a593Smuzhiyun 		goto no_dma;
3015*4882a593Smuzhiyun 	}
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun 	/* Determine which DMA interface to use */
3018*4882a593Smuzhiyun 	if (host->use_dma == TRANS_MODE_IDMAC) {
3019*4882a593Smuzhiyun 		/*
3020*4882a593Smuzhiyun 		* Check ADDR_CONFIG bit in HCON to find
3021*4882a593Smuzhiyun 		* IDMAC address bus width
3022*4882a593Smuzhiyun 		*/
3023*4882a593Smuzhiyun 		addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
3024*4882a593Smuzhiyun 
3025*4882a593Smuzhiyun 		if (addr_config == 1) {
3026*4882a593Smuzhiyun 			/* host supports IDMAC in 64-bit address mode */
3027*4882a593Smuzhiyun 			host->dma_64bit_address = 1;
3028*4882a593Smuzhiyun 			dev_info(host->dev,
3029*4882a593Smuzhiyun 				 "IDMAC supports 64-bit address mode.\n");
3030*4882a593Smuzhiyun 			if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
3031*4882a593Smuzhiyun 				dma_set_coherent_mask(host->dev,
3032*4882a593Smuzhiyun 						      DMA_BIT_MASK(64));
3033*4882a593Smuzhiyun 		} else {
3034*4882a593Smuzhiyun 			/* host supports IDMAC in 32-bit address mode */
3035*4882a593Smuzhiyun 			host->dma_64bit_address = 0;
3036*4882a593Smuzhiyun 			dev_info(host->dev,
3037*4882a593Smuzhiyun 				 "IDMAC supports 32-bit address mode.\n");
3038*4882a593Smuzhiyun 		}
3039*4882a593Smuzhiyun 
3040*4882a593Smuzhiyun 		/* Alloc memory for sg translation */
3041*4882a593Smuzhiyun 		host->sg_cpu = dmam_alloc_coherent(host->dev,
3042*4882a593Smuzhiyun 						   DESC_RING_BUF_SZ,
3043*4882a593Smuzhiyun 						   &host->sg_dma, GFP_KERNEL);
3044*4882a593Smuzhiyun 		if (!host->sg_cpu) {
3045*4882a593Smuzhiyun 			dev_err(host->dev,
3046*4882a593Smuzhiyun 				"%s: could not alloc DMA memory\n",
3047*4882a593Smuzhiyun 				__func__);
3048*4882a593Smuzhiyun 			goto no_dma;
3049*4882a593Smuzhiyun 		}
3050*4882a593Smuzhiyun 
3051*4882a593Smuzhiyun 		host->dma_ops = &dw_mci_idmac_ops;
3052*4882a593Smuzhiyun 		dev_info(host->dev, "Using internal DMA controller.\n");
3053*4882a593Smuzhiyun 	} else {
3054*4882a593Smuzhiyun 		/* TRANS_MODE_EDMAC: check dma bindings again */
3055*4882a593Smuzhiyun 		if ((device_property_read_string_array(dev, "dma-names",
3056*4882a593Smuzhiyun 						       NULL, 0) < 0) ||
3057*4882a593Smuzhiyun 		    !device_property_present(dev, "dmas")) {
3058*4882a593Smuzhiyun 			goto no_dma;
3059*4882a593Smuzhiyun 		}
3060*4882a593Smuzhiyun 		host->dma_ops = &dw_mci_edmac_ops;
3061*4882a593Smuzhiyun 		dev_info(host->dev, "Using external DMA controller.\n");
3062*4882a593Smuzhiyun 	}
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun 	if (host->dma_ops->init && host->dma_ops->start &&
3065*4882a593Smuzhiyun 	    host->dma_ops->stop && host->dma_ops->cleanup) {
3066*4882a593Smuzhiyun 		if (host->dma_ops->init(host)) {
3067*4882a593Smuzhiyun 			dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
3068*4882a593Smuzhiyun 				__func__);
3069*4882a593Smuzhiyun 			goto no_dma;
3070*4882a593Smuzhiyun 		}
3071*4882a593Smuzhiyun 	} else {
3072*4882a593Smuzhiyun 		dev_err(host->dev, "DMA initialization not found.\n");
3073*4882a593Smuzhiyun 		goto no_dma;
3074*4882a593Smuzhiyun 	}
3075*4882a593Smuzhiyun 
3076*4882a593Smuzhiyun 	return;
3077*4882a593Smuzhiyun 
3078*4882a593Smuzhiyun no_dma:
3079*4882a593Smuzhiyun 	dev_info(host->dev, "Using PIO mode.\n");
3080*4882a593Smuzhiyun 	host->use_dma = TRANS_MODE_PIO;
3081*4882a593Smuzhiyun }
3082*4882a593Smuzhiyun 
dw_mci_cmd11_timer(struct timer_list * t)3083*4882a593Smuzhiyun static void dw_mci_cmd11_timer(struct timer_list *t)
3084*4882a593Smuzhiyun {
3085*4882a593Smuzhiyun 	struct dw_mci *host = from_timer(host, t, cmd11_timer);
3086*4882a593Smuzhiyun 
3087*4882a593Smuzhiyun 	if (host->state != STATE_SENDING_CMD11) {
3088*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected CMD11 timeout\n");
3089*4882a593Smuzhiyun 		return;
3090*4882a593Smuzhiyun 	}
3091*4882a593Smuzhiyun 
3092*4882a593Smuzhiyun 	host->cmd_status = SDMMC_INT_RTO;
3093*4882a593Smuzhiyun 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
3094*4882a593Smuzhiyun 	tasklet_schedule(&host->tasklet);
3095*4882a593Smuzhiyun }
3096*4882a593Smuzhiyun 
dw_mci_cto_timer(struct timer_list * t)3097*4882a593Smuzhiyun static void dw_mci_cto_timer(struct timer_list *t)
3098*4882a593Smuzhiyun {
3099*4882a593Smuzhiyun 	struct dw_mci *host = from_timer(host, t, cto_timer);
3100*4882a593Smuzhiyun 	unsigned long irqflags;
3101*4882a593Smuzhiyun 	u32 pending;
3102*4882a593Smuzhiyun 
3103*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
3104*4882a593Smuzhiyun 
3105*4882a593Smuzhiyun 	/*
3106*4882a593Smuzhiyun 	 * If somehow we have very bad interrupt latency it's remotely possible
3107*4882a593Smuzhiyun 	 * that the timer could fire while the interrupt is still pending or
3108*4882a593Smuzhiyun 	 * while the interrupt is midway through running.  Let's be paranoid
3109*4882a593Smuzhiyun 	 * and detect those two cases.  Note that this is paranoia is somewhat
3110*4882a593Smuzhiyun 	 * justified because in this function we don't actually cancel the
3111*4882a593Smuzhiyun 	 * pending command in the controller--we just assume it will never come.
3112*4882a593Smuzhiyun 	 */
3113*4882a593Smuzhiyun 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
3114*4882a593Smuzhiyun 	if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {
3115*4882a593Smuzhiyun 		/* The interrupt should fire; no need to act but we can warn */
3116*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected interrupt latency\n");
3117*4882a593Smuzhiyun 		goto exit;
3118*4882a593Smuzhiyun 	}
3119*4882a593Smuzhiyun 	if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {
3120*4882a593Smuzhiyun 		/* Presumably interrupt handler couldn't delete the timer */
3121*4882a593Smuzhiyun 		dev_warn(host->dev, "CTO timeout when already completed\n");
3122*4882a593Smuzhiyun 		goto exit;
3123*4882a593Smuzhiyun 	}
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun 	/*
3126*4882a593Smuzhiyun 	 * Continued paranoia to make sure we're in the state we expect.
3127*4882a593Smuzhiyun 	 * This paranoia isn't really justified but it seems good to be safe.
3128*4882a593Smuzhiyun 	 */
3129*4882a593Smuzhiyun 	switch (host->state) {
3130*4882a593Smuzhiyun 	case STATE_SENDING_CMD11:
3131*4882a593Smuzhiyun 	case STATE_SENDING_CMD:
3132*4882a593Smuzhiyun 	case STATE_SENDING_STOP:
3133*4882a593Smuzhiyun 		/*
3134*4882a593Smuzhiyun 		 * If CMD_DONE interrupt does NOT come in sending command
3135*4882a593Smuzhiyun 		 * state, we should notify the driver to terminate current
3136*4882a593Smuzhiyun 		 * transfer and report a command timeout to the core.
3137*4882a593Smuzhiyun 		 */
3138*4882a593Smuzhiyun 		host->cmd_status = SDMMC_INT_RTO;
3139*4882a593Smuzhiyun 		set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
3140*4882a593Smuzhiyun 		tasklet_schedule(&host->tasklet);
3141*4882a593Smuzhiyun 		break;
3142*4882a593Smuzhiyun 	default:
3143*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected command timeout, state %d\n",
3144*4882a593Smuzhiyun 			 host->state);
3145*4882a593Smuzhiyun 		break;
3146*4882a593Smuzhiyun 	}
3147*4882a593Smuzhiyun 
3148*4882a593Smuzhiyun exit:
3149*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
3150*4882a593Smuzhiyun }
3151*4882a593Smuzhiyun 
dw_mci_xfer_timer(struct timer_list * t)3152*4882a593Smuzhiyun static void dw_mci_xfer_timer(struct timer_list *t)
3153*4882a593Smuzhiyun {
3154*4882a593Smuzhiyun 	struct dw_mci *host = from_timer(host, t, xfer_timer);
3155*4882a593Smuzhiyun 	unsigned long irqflags;
3156*4882a593Smuzhiyun 
3157*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
3158*4882a593Smuzhiyun 
3159*4882a593Smuzhiyun 	if (test_bit(EVENT_XFER_COMPLETE, &host->pending_events)) {
3160*4882a593Smuzhiyun 		/* Presumably interrupt handler couldn't delete the timer */
3161*4882a593Smuzhiyun 		dev_warn(host->dev, "xfer when already completed\n");
3162*4882a593Smuzhiyun 		goto exit;
3163*4882a593Smuzhiyun 	}
3164*4882a593Smuzhiyun 
3165*4882a593Smuzhiyun 	switch (host->state) {
3166*4882a593Smuzhiyun 	case STATE_SENDING_DATA:
3167*4882a593Smuzhiyun 		host->data_status = SDMMC_INT_DRTO;
3168*4882a593Smuzhiyun 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
3169*4882a593Smuzhiyun 		set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
3170*4882a593Smuzhiyun 		tasklet_schedule(&host->tasklet);
3171*4882a593Smuzhiyun 		break;
3172*4882a593Smuzhiyun 	default:
3173*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected xfer timeout, state %d\n",
3174*4882a593Smuzhiyun 			 host->state);
3175*4882a593Smuzhiyun 		break;
3176*4882a593Smuzhiyun 	}
3177*4882a593Smuzhiyun 
3178*4882a593Smuzhiyun exit:
3179*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
3180*4882a593Smuzhiyun }
3181*4882a593Smuzhiyun 
dw_mci_dto_timer(struct timer_list * t)3182*4882a593Smuzhiyun static void dw_mci_dto_timer(struct timer_list *t)
3183*4882a593Smuzhiyun {
3184*4882a593Smuzhiyun 	struct dw_mci *host = from_timer(host, t, dto_timer);
3185*4882a593Smuzhiyun 	unsigned long irqflags;
3186*4882a593Smuzhiyun 	u32 pending;
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun 	spin_lock_irqsave(&host->irq_lock, irqflags);
3189*4882a593Smuzhiyun 
3190*4882a593Smuzhiyun 	/*
3191*4882a593Smuzhiyun 	 * The DTO timer is much longer than the CTO timer, so it's even less
3192*4882a593Smuzhiyun 	 * likely that we'll these cases, but it pays to be paranoid.
3193*4882a593Smuzhiyun 	 */
3194*4882a593Smuzhiyun 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
3195*4882a593Smuzhiyun 	if (pending & SDMMC_INT_DATA_OVER) {
3196*4882a593Smuzhiyun 		/* The interrupt should fire; no need to act but we can warn */
3197*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected data interrupt latency\n");
3198*4882a593Smuzhiyun 		goto exit;
3199*4882a593Smuzhiyun 	}
3200*4882a593Smuzhiyun 	if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) {
3201*4882a593Smuzhiyun 		/* Presumably interrupt handler couldn't delete the timer */
3202*4882a593Smuzhiyun 		dev_warn(host->dev, "DTO timeout when already completed\n");
3203*4882a593Smuzhiyun 		goto exit;
3204*4882a593Smuzhiyun 	}
3205*4882a593Smuzhiyun 
3206*4882a593Smuzhiyun 	/*
3207*4882a593Smuzhiyun 	 * Continued paranoia to make sure we're in the state we expect.
3208*4882a593Smuzhiyun 	 * This paranoia isn't really justified but it seems good to be safe.
3209*4882a593Smuzhiyun 	 */
3210*4882a593Smuzhiyun 	switch (host->state) {
3211*4882a593Smuzhiyun 	case STATE_SENDING_DATA:
3212*4882a593Smuzhiyun 	case STATE_DATA_BUSY:
3213*4882a593Smuzhiyun 		/*
3214*4882a593Smuzhiyun 		 * If DTO interrupt does NOT come in sending data state,
3215*4882a593Smuzhiyun 		 * we should notify the driver to terminate current transfer
3216*4882a593Smuzhiyun 		 * and report a data timeout to the core.
3217*4882a593Smuzhiyun 		 */
3218*4882a593Smuzhiyun 		host->data_status = SDMMC_INT_DRTO;
3219*4882a593Smuzhiyun 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
3220*4882a593Smuzhiyun 		set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
3221*4882a593Smuzhiyun 		tasklet_schedule(&host->tasklet);
3222*4882a593Smuzhiyun 		break;
3223*4882a593Smuzhiyun 	default:
3224*4882a593Smuzhiyun 		dev_warn(host->dev, "Unexpected data timeout, state %d\n",
3225*4882a593Smuzhiyun 			 host->state);
3226*4882a593Smuzhiyun 		break;
3227*4882a593Smuzhiyun 	}
3228*4882a593Smuzhiyun 
3229*4882a593Smuzhiyun exit:
3230*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
3231*4882a593Smuzhiyun }
3232*4882a593Smuzhiyun 
3233*4882a593Smuzhiyun #ifdef CONFIG_OF
dw_mci_parse_dt(struct dw_mci * host)3234*4882a593Smuzhiyun static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3235*4882a593Smuzhiyun {
3236*4882a593Smuzhiyun 	struct dw_mci_board *pdata;
3237*4882a593Smuzhiyun 	struct device *dev = host->dev;
3238*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
3239*4882a593Smuzhiyun 	int ret;
3240*4882a593Smuzhiyun 	u32 clock_frequency;
3241*4882a593Smuzhiyun 
3242*4882a593Smuzhiyun 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3243*4882a593Smuzhiyun 	if (!pdata)
3244*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
3245*4882a593Smuzhiyun 
3246*4882a593Smuzhiyun 	/* find reset controller when exist */
3247*4882a593Smuzhiyun 	pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset");
3248*4882a593Smuzhiyun 	if (IS_ERR(pdata->rstc)) {
3249*4882a593Smuzhiyun 		if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
3250*4882a593Smuzhiyun 			return ERR_PTR(-EPROBE_DEFER);
3251*4882a593Smuzhiyun 	}
3252*4882a593Smuzhiyun 
3253*4882a593Smuzhiyun 	if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
3254*4882a593Smuzhiyun 		dev_info(dev,
3255*4882a593Smuzhiyun 			 "fifo-depth property not found, using value of FIFOTH register as default\n");
3256*4882a593Smuzhiyun 
3257*4882a593Smuzhiyun 	device_property_read_u32(dev, "card-detect-delay",
3258*4882a593Smuzhiyun 				 &pdata->detect_delay_ms);
3259*4882a593Smuzhiyun 
3260*4882a593Smuzhiyun 	device_property_read_u32(dev, "data-addr", &host->data_addr_override);
3261*4882a593Smuzhiyun 
3262*4882a593Smuzhiyun 	if (device_property_present(dev, "fifo-watermark-aligned"))
3263*4882a593Smuzhiyun 		host->wm_aligned = true;
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun 	if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
3266*4882a593Smuzhiyun 		pdata->bus_hz = clock_frequency;
3267*4882a593Smuzhiyun 
3268*4882a593Smuzhiyun 	if (drv_data && drv_data->parse_dt) {
3269*4882a593Smuzhiyun 		ret = drv_data->parse_dt(host);
3270*4882a593Smuzhiyun 		if (ret)
3271*4882a593Smuzhiyun 			return ERR_PTR(ret);
3272*4882a593Smuzhiyun 	}
3273*4882a593Smuzhiyun 
3274*4882a593Smuzhiyun 	host->pinctrl = devm_pinctrl_get(host->dev);
3275*4882a593Smuzhiyun 	if (!IS_ERR(host->pinctrl)) {
3276*4882a593Smuzhiyun 		host->normal_state = pinctrl_lookup_state(host->pinctrl, "normal");
3277*4882a593Smuzhiyun 		if (IS_ERR(host->normal_state))
3278*4882a593Smuzhiyun 			dev_warn(dev, "No normal pinctrl state\n");
3279*4882a593Smuzhiyun 
3280*4882a593Smuzhiyun 		host->idle_state = pinctrl_lookup_state(host->pinctrl, "idle");
3281*4882a593Smuzhiyun 		if (IS_ERR(host->idle_state))
3282*4882a593Smuzhiyun 			dev_warn(dev, "No idle pinctrl state\n");
3283*4882a593Smuzhiyun 
3284*4882a593Smuzhiyun 		if (!IS_ERR(host->normal_state) && !IS_ERR(host->idle_state))
3285*4882a593Smuzhiyun 			pinctrl_select_state(host->pinctrl, host->idle_state);
3286*4882a593Smuzhiyun 		else
3287*4882a593Smuzhiyun 			host->pinctrl = NULL;
3288*4882a593Smuzhiyun 	}
3289*4882a593Smuzhiyun 
3290*4882a593Smuzhiyun 	return pdata;
3291*4882a593Smuzhiyun }
3292*4882a593Smuzhiyun 
3293*4882a593Smuzhiyun #else /* CONFIG_OF */
dw_mci_parse_dt(struct dw_mci * host)3294*4882a593Smuzhiyun static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3295*4882a593Smuzhiyun {
3296*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
3297*4882a593Smuzhiyun }
3298*4882a593Smuzhiyun #endif /* CONFIG_OF */
3299*4882a593Smuzhiyun 
dw_mci_enable_cd(struct dw_mci * host)3300*4882a593Smuzhiyun static void dw_mci_enable_cd(struct dw_mci *host)
3301*4882a593Smuzhiyun {
3302*4882a593Smuzhiyun 	unsigned long irqflags;
3303*4882a593Smuzhiyun 	u32 temp;
3304*4882a593Smuzhiyun 
3305*4882a593Smuzhiyun 	/*
3306*4882a593Smuzhiyun 	 * No need for CD if all slots have a non-error GPIO
3307*4882a593Smuzhiyun 	 * as well as broken card detection is found.
3308*4882a593Smuzhiyun 	 */
3309*4882a593Smuzhiyun 	if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3310*4882a593Smuzhiyun 		return;
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun 	if (mmc_gpio_get_cd(host->slot->mmc) < 0) {
3313*4882a593Smuzhiyun 		spin_lock_irqsave(&host->irq_lock, irqflags);
3314*4882a593Smuzhiyun 		temp = mci_readl(host, INTMASK);
3315*4882a593Smuzhiyun 		temp  |= SDMMC_INT_CD;
3316*4882a593Smuzhiyun 		mci_writel(host, INTMASK, temp);
3317*4882a593Smuzhiyun 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
3318*4882a593Smuzhiyun 	}
3319*4882a593Smuzhiyun }
3320*4882a593Smuzhiyun 
dw_mci_probe(struct dw_mci * host)3321*4882a593Smuzhiyun int dw_mci_probe(struct dw_mci *host)
3322*4882a593Smuzhiyun {
3323*4882a593Smuzhiyun 	const struct dw_mci_drv_data *drv_data = host->drv_data;
3324*4882a593Smuzhiyun 	int width, i, ret = 0;
3325*4882a593Smuzhiyun 	u32 fifo_size;
3326*4882a593Smuzhiyun 
3327*4882a593Smuzhiyun 	if (!host->pdata) {
3328*4882a593Smuzhiyun 		host->pdata = dw_mci_parse_dt(host);
3329*4882a593Smuzhiyun 		if (IS_ERR(host->pdata))
3330*4882a593Smuzhiyun 			return dev_err_probe(host->dev, PTR_ERR(host->pdata),
3331*4882a593Smuzhiyun 					     "platform data not available\n");
3332*4882a593Smuzhiyun 	}
3333*4882a593Smuzhiyun 
3334*4882a593Smuzhiyun 	host->biu_clk = devm_clk_get(host->dev, "biu");
3335*4882a593Smuzhiyun 	if (IS_ERR(host->biu_clk)) {
3336*4882a593Smuzhiyun 		dev_dbg(host->dev, "biu clock not available\n");
3337*4882a593Smuzhiyun 	} else {
3338*4882a593Smuzhiyun 		ret = clk_prepare_enable(host->biu_clk);
3339*4882a593Smuzhiyun 		if (ret) {
3340*4882a593Smuzhiyun 			dev_err(host->dev, "failed to enable biu clock\n");
3341*4882a593Smuzhiyun 			return ret;
3342*4882a593Smuzhiyun 		}
3343*4882a593Smuzhiyun 	}
3344*4882a593Smuzhiyun 
3345*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
3346*4882a593Smuzhiyun 	if (device_property_read_bool(host->dev, "no-sd") &&
3347*4882a593Smuzhiyun 	    device_property_read_bool(host->dev, "no-sdio")) {
3348*4882a593Smuzhiyun 		if (readl_poll_timeout(host->regs + SDMMC_STATUS,
3349*4882a593Smuzhiyun 				fifo_size,
3350*4882a593Smuzhiyun 				!(fifo_size & (BIT(10) | GENMASK(7, 4))),
3351*4882a593Smuzhiyun 				0, 500 * USEC_PER_MSEC))
3352*4882a593Smuzhiyun 			dev_err(host->dev, "Controller is occupied!\n");
3353*4882a593Smuzhiyun 
3354*4882a593Smuzhiyun 		if (readl_poll_timeout(host->regs + SDMMC_IDSTS,
3355*4882a593Smuzhiyun 				fifo_size, !(fifo_size & GENMASK(16, 13)),
3356*4882a593Smuzhiyun 				0, 500 * USEC_PER_MSEC))
3357*4882a593Smuzhiyun 			dev_err(host->dev, "DMA is still running!\n");
3358*4882a593Smuzhiyun 
3359*4882a593Smuzhiyun 		BUG_ON(mci_readl(host, RINTSTS) & DW_MCI_ERROR_FLAGS);
3360*4882a593Smuzhiyun 	}
3361*4882a593Smuzhiyun #endif
3362*4882a593Smuzhiyun 
3363*4882a593Smuzhiyun 	host->ciu_clk = devm_clk_get(host->dev, "ciu");
3364*4882a593Smuzhiyun 	if (IS_ERR(host->ciu_clk)) {
3365*4882a593Smuzhiyun 		dev_dbg(host->dev, "ciu clock not available\n");
3366*4882a593Smuzhiyun 		host->bus_hz = host->pdata->bus_hz;
3367*4882a593Smuzhiyun 	} else {
3368*4882a593Smuzhiyun 		ret = clk_prepare_enable(host->ciu_clk);
3369*4882a593Smuzhiyun 		if (ret) {
3370*4882a593Smuzhiyun 			dev_err(host->dev, "failed to enable ciu clock\n");
3371*4882a593Smuzhiyun 			goto err_clk_biu;
3372*4882a593Smuzhiyun 		}
3373*4882a593Smuzhiyun 
3374*4882a593Smuzhiyun 		if (host->pdata->bus_hz) {
3375*4882a593Smuzhiyun 			ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3376*4882a593Smuzhiyun 			if (ret)
3377*4882a593Smuzhiyun 				dev_warn(host->dev,
3378*4882a593Smuzhiyun 					 "Unable to set bus rate to %uHz\n",
3379*4882a593Smuzhiyun 					 host->pdata->bus_hz);
3380*4882a593Smuzhiyun 		}
3381*4882a593Smuzhiyun 		host->bus_hz = clk_get_rate(host->ciu_clk);
3382*4882a593Smuzhiyun 	}
3383*4882a593Smuzhiyun 
3384*4882a593Smuzhiyun 	if (!host->bus_hz) {
3385*4882a593Smuzhiyun 		dev_err(host->dev,
3386*4882a593Smuzhiyun 			"Platform data must supply bus speed\n");
3387*4882a593Smuzhiyun 		ret = -ENODEV;
3388*4882a593Smuzhiyun 		goto err_clk_ciu;
3389*4882a593Smuzhiyun 	}
3390*4882a593Smuzhiyun 
3391*4882a593Smuzhiyun 	if (!IS_ERR(host->pdata->rstc)) {
3392*4882a593Smuzhiyun 		reset_control_assert(host->pdata->rstc);
3393*4882a593Smuzhiyun 		usleep_range(10, 50);
3394*4882a593Smuzhiyun 		reset_control_deassert(host->pdata->rstc);
3395*4882a593Smuzhiyun 	}
3396*4882a593Smuzhiyun 
3397*4882a593Smuzhiyun 	if (drv_data && drv_data->init) {
3398*4882a593Smuzhiyun 		ret = drv_data->init(host);
3399*4882a593Smuzhiyun 		if (ret) {
3400*4882a593Smuzhiyun 			dev_err(host->dev,
3401*4882a593Smuzhiyun 				"implementation specific init failed\n");
3402*4882a593Smuzhiyun 			goto err_clk_ciu;
3403*4882a593Smuzhiyun 		}
3404*4882a593Smuzhiyun 	}
3405*4882a593Smuzhiyun 
3406*4882a593Smuzhiyun 	timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0);
3407*4882a593Smuzhiyun 	timer_setup(&host->cto_timer, dw_mci_cto_timer, 0);
3408*4882a593Smuzhiyun 	timer_setup(&host->dto_timer, dw_mci_dto_timer, 0);
3409*4882a593Smuzhiyun 	if (host->need_xfer_timer)
3410*4882a593Smuzhiyun 		timer_setup(&host->xfer_timer, dw_mci_xfer_timer, 0);
3411*4882a593Smuzhiyun 
3412*4882a593Smuzhiyun 	spin_lock_init(&host->lock);
3413*4882a593Smuzhiyun 	spin_lock_init(&host->irq_lock);
3414*4882a593Smuzhiyun 	INIT_LIST_HEAD(&host->queue);
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun 	/*
3417*4882a593Smuzhiyun 	 * Get the host data width - this assumes that HCON has been set with
3418*4882a593Smuzhiyun 	 * the correct values.
3419*4882a593Smuzhiyun 	 */
3420*4882a593Smuzhiyun 	i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
3421*4882a593Smuzhiyun 	if (!i) {
3422*4882a593Smuzhiyun 		host->push_data = dw_mci_push_data16;
3423*4882a593Smuzhiyun 		host->pull_data = dw_mci_pull_data16;
3424*4882a593Smuzhiyun 		width = 16;
3425*4882a593Smuzhiyun 		host->data_shift = 1;
3426*4882a593Smuzhiyun 	} else if (i == 2) {
3427*4882a593Smuzhiyun 		host->push_data = dw_mci_push_data64;
3428*4882a593Smuzhiyun 		host->pull_data = dw_mci_pull_data64;
3429*4882a593Smuzhiyun 		width = 64;
3430*4882a593Smuzhiyun 		host->data_shift = 3;
3431*4882a593Smuzhiyun 	} else {
3432*4882a593Smuzhiyun 		/* Check for a reserved value, and warn if it is */
3433*4882a593Smuzhiyun 		WARN((i != 1),
3434*4882a593Smuzhiyun 		     "HCON reports a reserved host data width!\n"
3435*4882a593Smuzhiyun 		     "Defaulting to 32-bit access.\n");
3436*4882a593Smuzhiyun 		host->push_data = dw_mci_push_data32;
3437*4882a593Smuzhiyun 		host->pull_data = dw_mci_pull_data32;
3438*4882a593Smuzhiyun 		width = 32;
3439*4882a593Smuzhiyun 		host->data_shift = 2;
3440*4882a593Smuzhiyun 	}
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 	/* Reset all blocks */
3443*4882a593Smuzhiyun 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3444*4882a593Smuzhiyun 		ret = -ENODEV;
3445*4882a593Smuzhiyun 		goto err_clk_ciu;
3446*4882a593Smuzhiyun 	}
3447*4882a593Smuzhiyun 
3448*4882a593Smuzhiyun 	host->dma_ops = host->pdata->dma_ops;
3449*4882a593Smuzhiyun 	dw_mci_init_dma(host);
3450*4882a593Smuzhiyun 
3451*4882a593Smuzhiyun 	/* Clear the interrupts for the host controller */
3452*4882a593Smuzhiyun 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3453*4882a593Smuzhiyun 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3454*4882a593Smuzhiyun 
3455*4882a593Smuzhiyun 	/* Put in max timeout */
3456*4882a593Smuzhiyun 	mci_writel(host, TMOUT, 0xFFFFFFFF);
3457*4882a593Smuzhiyun 
3458*4882a593Smuzhiyun 	/*
3459*4882a593Smuzhiyun 	 * FIFO threshold settings  RxMark  = fifo_size / 2 - 1,
3460*4882a593Smuzhiyun 	 *                          Tx Mark = fifo_size / 2 DMA Size = 8
3461*4882a593Smuzhiyun 	 */
3462*4882a593Smuzhiyun 	if (!host->pdata->fifo_depth) {
3463*4882a593Smuzhiyun 		/*
3464*4882a593Smuzhiyun 		 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3465*4882a593Smuzhiyun 		 * have been overwritten by the bootloader, just like we're
3466*4882a593Smuzhiyun 		 * about to do, so if you know the value for your hardware, you
3467*4882a593Smuzhiyun 		 * should put it in the platform data.
3468*4882a593Smuzhiyun 		 */
3469*4882a593Smuzhiyun 		fifo_size = mci_readl(host, FIFOTH);
3470*4882a593Smuzhiyun 		fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
3471*4882a593Smuzhiyun 	} else {
3472*4882a593Smuzhiyun 		fifo_size = host->pdata->fifo_depth;
3473*4882a593Smuzhiyun 	}
3474*4882a593Smuzhiyun 	host->fifo_depth = fifo_size;
3475*4882a593Smuzhiyun 	host->fifoth_val =
3476*4882a593Smuzhiyun 		SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
3477*4882a593Smuzhiyun 	mci_writel(host, FIFOTH, host->fifoth_val);
3478*4882a593Smuzhiyun 
3479*4882a593Smuzhiyun 	/* disable clock to CIU */
3480*4882a593Smuzhiyun 	mci_writel(host, CLKENA, 0);
3481*4882a593Smuzhiyun 	mci_writel(host, CLKSRC, 0);
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 	/*
3484*4882a593Smuzhiyun 	 * In 2.40a spec, Data offset is changed.
3485*4882a593Smuzhiyun 	 * Need to check the version-id and set data-offset for DATA register.
3486*4882a593Smuzhiyun 	 */
3487*4882a593Smuzhiyun 	host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3488*4882a593Smuzhiyun 	dev_info(host->dev, "Version ID is %04x\n", host->verid);
3489*4882a593Smuzhiyun 
3490*4882a593Smuzhiyun 	if (host->data_addr_override)
3491*4882a593Smuzhiyun 		host->fifo_reg = host->regs + host->data_addr_override;
3492*4882a593Smuzhiyun 	else if (host->verid < DW_MMC_240A)
3493*4882a593Smuzhiyun 		host->fifo_reg = host->regs + DATA_OFFSET;
3494*4882a593Smuzhiyun 	else
3495*4882a593Smuzhiyun 		host->fifo_reg = host->regs + DATA_240A_OFFSET;
3496*4882a593Smuzhiyun 
3497*4882a593Smuzhiyun 	tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
3498*4882a593Smuzhiyun 	ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3499*4882a593Smuzhiyun 			       host->irq_flags, "dw-mci", host);
3500*4882a593Smuzhiyun 	if (ret)
3501*4882a593Smuzhiyun 		goto err_dmaunmap;
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun 	/*
3504*4882a593Smuzhiyun 	 * Enable interrupts for command done, data over, data empty,
3505*4882a593Smuzhiyun 	 * receive ready and error such as transmit, receive timeout, crc error
3506*4882a593Smuzhiyun 	 */
3507*4882a593Smuzhiyun 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3508*4882a593Smuzhiyun 		   SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3509*4882a593Smuzhiyun 		   DW_MCI_ERROR_FLAGS);
3510*4882a593Smuzhiyun 	/* Enable mci interrupt */
3511*4882a593Smuzhiyun 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun 	dev_info(host->dev,
3514*4882a593Smuzhiyun 		 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
3515*4882a593Smuzhiyun 		 host->irq, width, fifo_size);
3516*4882a593Smuzhiyun 
3517*4882a593Smuzhiyun 	/* We need at least one slot to succeed */
3518*4882a593Smuzhiyun 	ret = dw_mci_init_slot(host);
3519*4882a593Smuzhiyun 	if (ret) {
3520*4882a593Smuzhiyun 		dev_dbg(host->dev, "slot %d init failed\n", i);
3521*4882a593Smuzhiyun 		goto err_dmaunmap;
3522*4882a593Smuzhiyun 	}
3523*4882a593Smuzhiyun 
3524*4882a593Smuzhiyun 	if (host->is_rv1106_sd) {
3525*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_CPU_RV1106)
3526*4882a593Smuzhiyun 		g_sdmmc_ispvicap_lock = &host->lock;
3527*4882a593Smuzhiyun #endif
3528*4882a593Smuzhiyun 		/* Select IDMAC interface */
3529*4882a593Smuzhiyun 		fifo_size = mci_readl(host, CTRL);
3530*4882a593Smuzhiyun 		fifo_size |= SDMMC_CTRL_USE_IDMAC;
3531*4882a593Smuzhiyun 		mci_writel(host, CTRL, fifo_size);
3532*4882a593Smuzhiyun 
3533*4882a593Smuzhiyun 		fifo_size = mci_readl(host, INTMASK);
3534*4882a593Smuzhiyun 		fifo_size &= ~SDMMC_INT_HTO;
3535*4882a593Smuzhiyun 		mci_writel(host, INTMASK, fifo_size);
3536*4882a593Smuzhiyun 
3537*4882a593Smuzhiyun 		host->slot->mmc->caps &= ~(MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
3538*4882a593Smuzhiyun 					   MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
3539*4882a593Smuzhiyun 					   MMC_CAP_UHS_SDR12);
3540*4882a593Smuzhiyun 	}
3541*4882a593Smuzhiyun 
3542*4882a593Smuzhiyun 	/* Now that slots are all setup, we can enable card detect */
3543*4882a593Smuzhiyun 	dw_mci_enable_cd(host);
3544*4882a593Smuzhiyun 
3545*4882a593Smuzhiyun 	return 0;
3546*4882a593Smuzhiyun 
3547*4882a593Smuzhiyun err_dmaunmap:
3548*4882a593Smuzhiyun 	if (host->use_dma && host->dma_ops->exit)
3549*4882a593Smuzhiyun 		host->dma_ops->exit(host);
3550*4882a593Smuzhiyun 
3551*4882a593Smuzhiyun 	if (!IS_ERR(host->pdata->rstc))
3552*4882a593Smuzhiyun 		reset_control_assert(host->pdata->rstc);
3553*4882a593Smuzhiyun 
3554*4882a593Smuzhiyun err_clk_ciu:
3555*4882a593Smuzhiyun 	clk_disable_unprepare(host->ciu_clk);
3556*4882a593Smuzhiyun 
3557*4882a593Smuzhiyun err_clk_biu:
3558*4882a593Smuzhiyun 	clk_disable_unprepare(host->biu_clk);
3559*4882a593Smuzhiyun 
3560*4882a593Smuzhiyun 	return ret;
3561*4882a593Smuzhiyun }
3562*4882a593Smuzhiyun EXPORT_SYMBOL(dw_mci_probe);
3563*4882a593Smuzhiyun 
dw_mci_remove(struct dw_mci * host)3564*4882a593Smuzhiyun void dw_mci_remove(struct dw_mci *host)
3565*4882a593Smuzhiyun {
3566*4882a593Smuzhiyun 	dev_dbg(host->dev, "remove slot\n");
3567*4882a593Smuzhiyun 	if (host->slot)
3568*4882a593Smuzhiyun 		dw_mci_cleanup_slot(host->slot);
3569*4882a593Smuzhiyun 
3570*4882a593Smuzhiyun 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3571*4882a593Smuzhiyun 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3572*4882a593Smuzhiyun 
3573*4882a593Smuzhiyun 	/* disable clock to CIU */
3574*4882a593Smuzhiyun 	mci_writel(host, CLKENA, 0);
3575*4882a593Smuzhiyun 	mci_writel(host, CLKSRC, 0);
3576*4882a593Smuzhiyun 
3577*4882a593Smuzhiyun 	if (host->use_dma && host->dma_ops->exit)
3578*4882a593Smuzhiyun 		host->dma_ops->exit(host);
3579*4882a593Smuzhiyun 
3580*4882a593Smuzhiyun 	if (!IS_ERR(host->pdata->rstc))
3581*4882a593Smuzhiyun 		reset_control_assert(host->pdata->rstc);
3582*4882a593Smuzhiyun 
3583*4882a593Smuzhiyun 	clk_disable_unprepare(host->ciu_clk);
3584*4882a593Smuzhiyun 	clk_disable_unprepare(host->biu_clk);
3585*4882a593Smuzhiyun }
3586*4882a593Smuzhiyun EXPORT_SYMBOL(dw_mci_remove);
3587*4882a593Smuzhiyun 
3588*4882a593Smuzhiyun 
3589*4882a593Smuzhiyun 
3590*4882a593Smuzhiyun #ifdef CONFIG_PM
dw_mci_runtime_suspend(struct device * dev)3591*4882a593Smuzhiyun int dw_mci_runtime_suspend(struct device *dev)
3592*4882a593Smuzhiyun {
3593*4882a593Smuzhiyun 	struct dw_mci *host = dev_get_drvdata(dev);
3594*4882a593Smuzhiyun 
3595*4882a593Smuzhiyun 	if (host->use_dma && host->dma_ops->exit)
3596*4882a593Smuzhiyun 		host->dma_ops->exit(host);
3597*4882a593Smuzhiyun 
3598*4882a593Smuzhiyun 	clk_disable_unprepare(host->ciu_clk);
3599*4882a593Smuzhiyun 
3600*4882a593Smuzhiyun 	if (host->slot &&
3601*4882a593Smuzhiyun 	    (mmc_can_gpio_cd(host->slot->mmc) ||
3602*4882a593Smuzhiyun 	     !mmc_card_is_removable(host->slot->mmc)))
3603*4882a593Smuzhiyun 		clk_disable_unprepare(host->biu_clk);
3604*4882a593Smuzhiyun 
3605*4882a593Smuzhiyun 	return 0;
3606*4882a593Smuzhiyun }
3607*4882a593Smuzhiyun EXPORT_SYMBOL(dw_mci_runtime_suspend);
3608*4882a593Smuzhiyun 
dw_mci_runtime_resume(struct device * dev)3609*4882a593Smuzhiyun int dw_mci_runtime_resume(struct device *dev)
3610*4882a593Smuzhiyun {
3611*4882a593Smuzhiyun 	int ret = 0;
3612*4882a593Smuzhiyun 	struct dw_mci *host = dev_get_drvdata(dev);
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun 	if (host->slot &&
3615*4882a593Smuzhiyun 	    (mmc_can_gpio_cd(host->slot->mmc) ||
3616*4882a593Smuzhiyun 	     !mmc_card_is_removable(host->slot->mmc))) {
3617*4882a593Smuzhiyun 		ret = clk_prepare_enable(host->biu_clk);
3618*4882a593Smuzhiyun 		if (ret)
3619*4882a593Smuzhiyun 			return ret;
3620*4882a593Smuzhiyun 	}
3621*4882a593Smuzhiyun 
3622*4882a593Smuzhiyun 	ret = clk_prepare_enable(host->ciu_clk);
3623*4882a593Smuzhiyun 	if (ret)
3624*4882a593Smuzhiyun 		goto err;
3625*4882a593Smuzhiyun 
3626*4882a593Smuzhiyun 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3627*4882a593Smuzhiyun 		clk_disable_unprepare(host->ciu_clk);
3628*4882a593Smuzhiyun 		ret = -ENODEV;
3629*4882a593Smuzhiyun 		goto err;
3630*4882a593Smuzhiyun 	}
3631*4882a593Smuzhiyun 
3632*4882a593Smuzhiyun 	if (host->use_dma && host->dma_ops->init)
3633*4882a593Smuzhiyun 		host->dma_ops->init(host);
3634*4882a593Smuzhiyun 
3635*4882a593Smuzhiyun 	/*
3636*4882a593Smuzhiyun 	 * Restore the initial value at FIFOTH register
3637*4882a593Smuzhiyun 	 * And Invalidate the prev_blksz with zero
3638*4882a593Smuzhiyun 	 */
3639*4882a593Smuzhiyun 	mci_writel(host, FIFOTH, host->fifoth_val);
3640*4882a593Smuzhiyun 	host->prev_blksz = 0;
3641*4882a593Smuzhiyun 
3642*4882a593Smuzhiyun 	/* Put in max timeout */
3643*4882a593Smuzhiyun 	mci_writel(host, TMOUT, 0xFFFFFFFF);
3644*4882a593Smuzhiyun 
3645*4882a593Smuzhiyun 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3646*4882a593Smuzhiyun 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | DW_MCI_ERROR_FLAGS);
3647*4882a593Smuzhiyun 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3648*4882a593Smuzhiyun 
3649*4882a593Smuzhiyun 	if (host->is_rv1106_sd) {
3650*4882a593Smuzhiyun 		/* Select IDMAC interface */
3651*4882a593Smuzhiyun 		ret = mci_readl(host, CTRL);
3652*4882a593Smuzhiyun 		ret |= SDMMC_CTRL_USE_IDMAC;
3653*4882a593Smuzhiyun 		mci_writel(host, CTRL, ret);
3654*4882a593Smuzhiyun 
3655*4882a593Smuzhiyun 		ret = mci_readl(host, INTMASK);
3656*4882a593Smuzhiyun 		ret &= ~SDMMC_INT_HTO;
3657*4882a593Smuzhiyun 		mci_writel(host, INTMASK, ret);
3658*4882a593Smuzhiyun 	}
3659*4882a593Smuzhiyun 
3660*4882a593Smuzhiyun 	if (host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
3661*4882a593Smuzhiyun 		dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);
3662*4882a593Smuzhiyun 
3663*4882a593Smuzhiyun 	/* Force setup bus to guarantee available clock output */
3664*4882a593Smuzhiyun 	dw_mci_setup_bus(host->slot, true);
3665*4882a593Smuzhiyun 
3666*4882a593Smuzhiyun 	/* Re-enable SDIO interrupts. */
3667*4882a593Smuzhiyun 	if (sdio_irq_claimed(host->slot->mmc))
3668*4882a593Smuzhiyun 		__dw_mci_enable_sdio_irq(host->slot, 1);
3669*4882a593Smuzhiyun 
3670*4882a593Smuzhiyun 	/* Now that slots are all setup, we can enable card detect */
3671*4882a593Smuzhiyun 	dw_mci_enable_cd(host);
3672*4882a593Smuzhiyun 
3673*4882a593Smuzhiyun 	return 0;
3674*4882a593Smuzhiyun 
3675*4882a593Smuzhiyun err:
3676*4882a593Smuzhiyun 	if (host->slot &&
3677*4882a593Smuzhiyun 	    (mmc_can_gpio_cd(host->slot->mmc) ||
3678*4882a593Smuzhiyun 	     !mmc_card_is_removable(host->slot->mmc)))
3679*4882a593Smuzhiyun 		clk_disable_unprepare(host->biu_clk);
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 	return ret;
3682*4882a593Smuzhiyun }
3683*4882a593Smuzhiyun EXPORT_SYMBOL(dw_mci_runtime_resume);
3684*4882a593Smuzhiyun #endif /* CONFIG_PM */
3685*4882a593Smuzhiyun 
dw_mci_init(void)3686*4882a593Smuzhiyun static int __init dw_mci_init(void)
3687*4882a593Smuzhiyun {
3688*4882a593Smuzhiyun 	pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3689*4882a593Smuzhiyun 	return 0;
3690*4882a593Smuzhiyun }
3691*4882a593Smuzhiyun 
dw_mci_exit(void)3692*4882a593Smuzhiyun static void __exit dw_mci_exit(void)
3693*4882a593Smuzhiyun {
3694*4882a593Smuzhiyun }
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun module_init(dw_mci_init);
3697*4882a593Smuzhiyun module_exit(dw_mci_exit);
3698*4882a593Smuzhiyun 
3699*4882a593Smuzhiyun MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3700*4882a593Smuzhiyun MODULE_AUTHOR("NXP Semiconductor VietNam");
3701*4882a593Smuzhiyun MODULE_AUTHOR("Imagination Technologies Ltd");
3702*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
3703