blob: 1b415da82754f6d289014e2f5adfb261bdecf5c4 [file] [log] [blame]
Will Newtonf95f3852011-01-02 01:11:59 -05001/*
2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
4 *
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/blkdev.h>
15#include <linux/clk.h>
16#include <linux/debugfs.h>
17#include <linux/device.h>
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
Shawn Linb6d2d812017-02-17 10:56:39 +080022#include <linux/iopoll.h>
Will Newtonf95f3852011-01-02 01:11:59 -050023#include <linux/ioport.h>
24#include <linux/module.h>
25#include <linux/platform_device.h>
Douglas Andersona6db2c82017-04-11 15:55:43 -070026#include <linux/pm_runtime.h>
Will Newtonf95f3852011-01-02 01:11:59 -050027#include <linux/seq_file.h>
28#include <linux/slab.h>
29#include <linux/stat.h>
30#include <linux/delay.h>
31#include <linux/irq.h>
Doug Andersonb24c8b22014-12-02 15:42:46 -080032#include <linux/mmc/card.h>
Will Newtonf95f3852011-01-02 01:11:59 -050033#include <linux/mmc/host.h>
34#include <linux/mmc/mmc.h>
Doug Anderson01730552014-08-22 19:17:51 +053035#include <linux/mmc/sd.h>
Seungwon Jeon90c21432013-08-31 00:14:05 +090036#include <linux/mmc/sdio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050037#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090038#include <linux/regulator/consumer.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000039#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000040#include <linux/of_gpio.h>
Zhangfei Gaobf626e52014-01-09 22:35:10 +080041#include <linux/mmc/slot-gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050042
43#include "dw_mmc.h"
44
45/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090046#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050047 SDMMC_INT_HTO | SDMMC_INT_SBE | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070048 SDMMC_INT_EBE | SDMMC_INT_HLE)
Will Newtonf95f3852011-01-02 01:11:59 -050049#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070050 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
Will Newtonf95f3852011-01-02 01:11:59 -050051#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070052 DW_MCI_CMD_ERROR_FLAGS)
Will Newtonf95f3852011-01-02 01:11:59 -050053#define DW_MCI_SEND_STATUS 1
54#define DW_MCI_RECV_STATUS 2
55#define DW_MCI_DMA_THRESHOLD 16
56
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090057#define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
Jaehoon Chung72e83572016-11-17 16:40:35 +090058#define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090059
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +090060#define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
61 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
62 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
63 SDMMC_IDMAC_INT_TI)
64
Shawn Lincc190d42016-09-02 12:14:39 +080065#define DESC_RING_BUF_SZ PAGE_SIZE
66
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000067struct idmac_desc_64addr {
68 u32 des0; /* Control Descriptor */
Shawn Linb6d2d812017-02-17 10:56:39 +080069#define IDMAC_OWN_CLR64(x) \
70 !((x) & cpu_to_le32(IDMAC_DES0_OWN))
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000071
72 u32 des1; /* Reserved */
73
74 u32 des2; /*Buffer sizes */
75#define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
Ben Dooks6687c422015-03-25 11:27:51 +000076 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
77 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000078
79 u32 des3; /* Reserved */
80
81 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
82 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
83
84 u32 des6; /* Lower 32-bits of Next Descriptor Address */
85 u32 des7; /* Upper 32-bits of Next Descriptor Address */
86};
87
Will Newtonf95f3852011-01-02 01:11:59 -050088struct idmac_desc {
Ben Dooks6687c422015-03-25 11:27:51 +000089 __le32 des0; /* Control Descriptor */
Will Newtonf95f3852011-01-02 01:11:59 -050090#define IDMAC_DES0_DIC BIT(1)
91#define IDMAC_DES0_LD BIT(2)
92#define IDMAC_DES0_FD BIT(3)
93#define IDMAC_DES0_CH BIT(4)
94#define IDMAC_DES0_ER BIT(5)
95#define IDMAC_DES0_CES BIT(30)
96#define IDMAC_DES0_OWN BIT(31)
97
Ben Dooks6687c422015-03-25 11:27:51 +000098 __le32 des1; /* Buffer sizes */
Will Newtonf95f3852011-01-02 01:11:59 -050099#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Ben Dookse5306c32016-06-07 14:37:19 +0100100 ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
Will Newtonf95f3852011-01-02 01:11:59 -0500101
Ben Dooks6687c422015-03-25 11:27:51 +0000102 __le32 des2; /* buffer 1 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -0500103
Ben Dooks6687c422015-03-25 11:27:51 +0000104 __le32 des3; /* buffer 2 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -0500105};
Alexey Brodkin5959b322015-06-25 11:25:07 +0300106
107/* Each descriptor can transfer up to 4KB of data in chained mode */
108#define DW_MCI_DESC_DATA_LENGTH 0x1000
Will Newtonf95f3852011-01-02 01:11:59 -0500109
Will Newtonf95f3852011-01-02 01:11:59 -0500110#if defined(CONFIG_DEBUG_FS)
111static int dw_mci_req_show(struct seq_file *s, void *v)
112{
113 struct dw_mci_slot *slot = s->private;
114 struct mmc_request *mrq;
115 struct mmc_command *cmd;
116 struct mmc_command *stop;
117 struct mmc_data *data;
118
119 /* Make sure we get a consistent snapshot */
120 spin_lock_bh(&slot->host->lock);
121 mrq = slot->mrq;
122
123 if (mrq) {
124 cmd = mrq->cmd;
125 data = mrq->data;
126 stop = mrq->stop;
127
128 if (cmd)
129 seq_printf(s,
130 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
131 cmd->opcode, cmd->arg, cmd->flags,
132 cmd->resp[0], cmd->resp[1], cmd->resp[2],
133 cmd->resp[2], cmd->error);
134 if (data)
135 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
136 data->bytes_xfered, data->blocks,
137 data->blksz, data->flags, data->error);
138 if (stop)
139 seq_printf(s,
140 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
141 stop->opcode, stop->arg, stop->flags,
142 stop->resp[0], stop->resp[1], stop->resp[2],
143 stop->resp[2], stop->error);
144 }
145
146 spin_unlock_bh(&slot->host->lock);
147
148 return 0;
149}
150
151static int dw_mci_req_open(struct inode *inode, struct file *file)
152{
153 return single_open(file, dw_mci_req_show, inode->i_private);
154}
155
156static const struct file_operations dw_mci_req_fops = {
157 .owner = THIS_MODULE,
158 .open = dw_mci_req_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = single_release,
162};
163
164static int dw_mci_regs_show(struct seq_file *s, void *v)
165{
Jaehoon Chung21657ebd2016-11-17 16:40:33 +0900166 struct dw_mci *host = s->private;
167
168 seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
169 seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
170 seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
171 seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
172 seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
173 seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
Will Newtonf95f3852011-01-02 01:11:59 -0500174
175 return 0;
176}
177
178static int dw_mci_regs_open(struct inode *inode, struct file *file)
179{
180 return single_open(file, dw_mci_regs_show, inode->i_private);
181}
182
183static const struct file_operations dw_mci_regs_fops = {
184 .owner = THIS_MODULE,
185 .open = dw_mci_regs_open,
186 .read = seq_read,
187 .llseek = seq_lseek,
188 .release = single_release,
189};
190
191static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
192{
193 struct mmc_host *mmc = slot->mmc;
194 struct dw_mci *host = slot->host;
195 struct dentry *root;
196 struct dentry *node;
197
198 root = mmc->debugfs_root;
199 if (!root)
200 return;
201
202 node = debugfs_create_file("regs", S_IRUSR, root, host,
203 &dw_mci_regs_fops);
204 if (!node)
205 goto err;
206
207 node = debugfs_create_file("req", S_IRUSR, root, slot,
208 &dw_mci_req_fops);
209 if (!node)
210 goto err;
211
212 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
213 if (!node)
214 goto err;
215
216 node = debugfs_create_x32("pending_events", S_IRUSR, root,
217 (u32 *)&host->pending_events);
218 if (!node)
219 goto err;
220
221 node = debugfs_create_x32("completed_events", S_IRUSR, root,
222 (u32 *)&host->completed_events);
223 if (!node)
224 goto err;
225
226 return;
227
228err:
229 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
230}
231#endif /* defined(CONFIG_DEBUG_FS) */
232
Shawn Lin8e6db1f2017-02-17 10:56:41 +0800233static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
234{
235 u32 ctrl;
236
237 ctrl = mci_readl(host, CTRL);
238 ctrl |= reset;
239 mci_writel(host, CTRL, ctrl);
240
241 /* wait till resets clear */
242 if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
243 !(ctrl & reset),
244 1, 500 * USEC_PER_MSEC)) {
245 dev_err(host->dev,
246 "Timeout resetting block (ctrl reset %#x)\n",
247 ctrl & reset);
248 return false;
249 }
250
251 return true;
252}
Doug Anderson01730552014-08-22 19:17:51 +0530253
Shawn Lin4dba18d2017-02-17 10:59:44 +0800254static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
255{
256 u32 status;
257
258 /*
259 * Databook says that before issuing a new data transfer command
260 * we need to check to see if the card is busy. Data transfer commands
261 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
262 *
263 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
264 * expected.
265 */
266 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
267 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
268 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
269 status,
270 !(status & SDMMC_STATUS_BUSY),
271 10, 500 * USEC_PER_MSEC))
272 dev_err(host->dev, "Busy; trying anyway\n");
273 }
274}
275
276static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
277{
278 struct dw_mci *host = slot->host;
279 unsigned int cmd_status = 0;
280
281 mci_writel(host, CMDARG, arg);
282 wmb(); /* drain writebuffer */
283 dw_mci_wait_while_busy(host, cmd);
284 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
285
286 if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
287 !(cmd_status & SDMMC_CMD_START),
288 1, 500 * USEC_PER_MSEC))
289 dev_err(&slot->mmc->class_dev,
290 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
291 cmd, arg, cmd_status);
292}
293
Will Newtonf95f3852011-01-02 01:11:59 -0500294static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
295{
Thomas Abraham800d78b2012-09-17 18:16:42 +0000296 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530297 struct dw_mci *host = slot->host;
Will Newtonf95f3852011-01-02 01:11:59 -0500298 u32 cmdr;
Will Newtonf95f3852011-01-02 01:11:59 -0500299
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800300 cmd->error = -EINPROGRESS;
Will Newtonf95f3852011-01-02 01:11:59 -0500301 cmdr = cmd->opcode;
302
Seungwon Jeon90c21432013-08-31 00:14:05 +0900303 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
304 cmd->opcode == MMC_GO_IDLE_STATE ||
305 cmd->opcode == MMC_GO_INACTIVE_STATE ||
306 (cmd->opcode == SD_IO_RW_DIRECT &&
307 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500308 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900309 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
310 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500311
Doug Anderson01730552014-08-22 19:17:51 +0530312 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
313 u32 clk_en_a;
314
315 /* Special bit makes CMD11 not die */
316 cmdr |= SDMMC_CMD_VOLT_SWITCH;
317
318 /* Change state to continue to handle CMD11 weirdness */
319 WARN_ON(slot->host->state != STATE_SENDING_CMD);
320 slot->host->state = STATE_SENDING_CMD11;
321
322 /*
323 * We need to disable low power mode (automatic clock stop)
324 * while doing voltage switch so we don't confuse the card,
325 * since stopping the clock is a specific part of the UHS
326 * voltage change dance.
327 *
328 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
329 * unconditionally turned back on in dw_mci_setup_bus() if it's
330 * ever called with a non-zero clock. That shouldn't happen
331 * until the voltage change is all done.
332 */
333 clk_en_a = mci_readl(host, CLKENA);
334 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
335 mci_writel(host, CLKENA, clk_en_a);
336 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
337 SDMMC_CMD_PRV_DAT_WAIT, 0);
338 }
339
Will Newtonf95f3852011-01-02 01:11:59 -0500340 if (cmd->flags & MMC_RSP_PRESENT) {
341 /* We expect a response, so set this bit */
342 cmdr |= SDMMC_CMD_RESP_EXP;
343 if (cmd->flags & MMC_RSP_136)
344 cmdr |= SDMMC_CMD_RESP_LONG;
345 }
346
347 if (cmd->flags & MMC_RSP_CRC)
348 cmdr |= SDMMC_CMD_RESP_CRC;
349
Jaehoon Chung0349c082016-11-17 16:40:39 +0900350 if (cmd->data) {
Will Newtonf95f3852011-01-02 01:11:59 -0500351 cmdr |= SDMMC_CMD_DAT_EXP;
Jaehoon Chung0349c082016-11-17 16:40:39 +0900352 if (cmd->data->flags & MMC_DATA_WRITE)
Will Newtonf95f3852011-01-02 01:11:59 -0500353 cmdr |= SDMMC_CMD_DAT_WR;
354 }
355
Jaehoon Chungaaaaeb72016-01-21 11:01:06 +0900356 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
357 cmdr |= SDMMC_CMD_USE_HOLD_REG;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000358
Will Newtonf95f3852011-01-02 01:11:59 -0500359 return cmdr;
360}
361
Seungwon Jeon90c21432013-08-31 00:14:05 +0900362static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
363{
364 struct mmc_command *stop;
365 u32 cmdr;
366
367 if (!cmd->data)
368 return 0;
369
370 stop = &host->stop_abort;
371 cmdr = cmd->opcode;
372 memset(stop, 0, sizeof(struct mmc_command));
373
374 if (cmdr == MMC_READ_SINGLE_BLOCK ||
375 cmdr == MMC_READ_MULTIPLE_BLOCK ||
376 cmdr == MMC_WRITE_BLOCK ||
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100377 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
378 cmdr == MMC_SEND_TUNING_BLOCK ||
379 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
Seungwon Jeon90c21432013-08-31 00:14:05 +0900380 stop->opcode = MMC_STOP_TRANSMISSION;
381 stop->arg = 0;
382 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
383 } else if (cmdr == SD_IO_RW_EXTENDED) {
384 stop->opcode = SD_IO_RW_DIRECT;
385 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
386 ((cmd->arg >> 28) & 0x7);
387 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
388 } else {
389 return 0;
390 }
391
392 cmdr = stop->opcode | SDMMC_CMD_STOP |
393 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
394
Jaehoon Chung8c005b42016-11-17 16:40:36 +0900395 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags))
396 cmdr |= SDMMC_CMD_USE_HOLD_REG;
397
Seungwon Jeon90c21432013-08-31 00:14:05 +0900398 return cmdr;
399}
400
Will Newtonf95f3852011-01-02 01:11:59 -0500401static void dw_mci_start_command(struct dw_mci *host,
402 struct mmc_command *cmd, u32 cmd_flags)
403{
404 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000405 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500406 "start command: ARGR=0x%08x CMDR=0x%08x\n",
407 cmd->arg, cmd_flags);
408
409 mci_writel(host, CMDARG, cmd->arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800410 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800411 dw_mci_wait_while_busy(host, cmd_flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500412
413 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
414}
415
Seungwon Jeon90c21432013-08-31 00:14:05 +0900416static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500417{
Jaehoon Chunge13c3c02016-11-17 16:40:37 +0900418 struct mmc_command *stop = &host->stop_abort;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800419
Seungwon Jeon90c21432013-08-31 00:14:05 +0900420 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500421}
422
423/* DMA interface functions */
424static void dw_mci_stop_dma(struct dw_mci *host)
425{
James Hogan03e8cb52011-06-29 09:28:43 +0100426 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500427 host->dma_ops->stop(host);
428 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500429 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900430
431 /* Data transfer was stopped by the interrupt handler */
432 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500433}
434
Will Newtonf95f3852011-01-02 01:11:59 -0500435static void dw_mci_dma_cleanup(struct dw_mci *host)
436{
437 struct mmc_data *data = host->data;
438
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900439 if (data && data->host_cookie == COOKIE_MAPPED) {
440 dma_unmap_sg(host->dev,
441 data->sg,
442 data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200443 mmc_get_dma_dir(data));
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900444 data->host_cookie = COOKIE_UNMAPPED;
445 }
Will Newtonf95f3852011-01-02 01:11:59 -0500446}
447
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900448static void dw_mci_idmac_reset(struct dw_mci *host)
449{
450 u32 bmod = mci_readl(host, BMOD);
451 /* Software reset of DMA */
452 bmod |= SDMMC_IDMAC_SWRESET;
453 mci_writel(host, BMOD, bmod);
454}
455
Will Newtonf95f3852011-01-02 01:11:59 -0500456static void dw_mci_idmac_stop_dma(struct dw_mci *host)
457{
458 u32 temp;
459
460 /* Disable and reset the IDMAC interface */
461 temp = mci_readl(host, CTRL);
462 temp &= ~SDMMC_CTRL_USE_IDMAC;
463 temp |= SDMMC_CTRL_DMA_RESET;
464 mci_writel(host, CTRL, temp);
465
466 /* Stop the IDMAC running */
467 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900468 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900469 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500470 mci_writel(host, BMOD, temp);
471}
472
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800473static void dw_mci_dmac_complete_dma(void *arg)
Will Newtonf95f3852011-01-02 01:11:59 -0500474{
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800475 struct dw_mci *host = arg;
Will Newtonf95f3852011-01-02 01:11:59 -0500476 struct mmc_data *data = host->data;
477
Thomas Abraham4a909202012-09-17 18:16:35 +0000478 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500479
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800480 if ((host->use_dma == TRANS_MODE_EDMAC) &&
481 data && (data->flags & MMC_DATA_READ))
482 /* Invalidate cache after read */
483 dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
484 data->sg,
485 data->sg_len,
486 DMA_FROM_DEVICE);
487
Will Newtonf95f3852011-01-02 01:11:59 -0500488 host->dma_ops->cleanup(host);
489
490 /*
491 * If the card was removed, data will be NULL. No point in trying to
492 * send the stop command or waiting for NBUSY in this case.
493 */
494 if (data) {
495 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
496 tasklet_schedule(&host->tasklet);
497 }
498}
499
Will Newtonf95f3852011-01-02 01:11:59 -0500500static int dw_mci_idmac_init(struct dw_mci *host)
501{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800502 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500503
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000504 if (host->dma_64bit_address == 1) {
505 struct idmac_desc_64addr *p;
506 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800507 host->ring_size =
508 DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500509
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000510 /* Forward link the descriptor list */
511 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
512 i++, p++) {
513 p->des6 = (host->sg_dma +
514 (sizeof(struct idmac_desc_64addr) *
515 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500516
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000517 p->des7 = (u64)(host->sg_dma +
518 (sizeof(struct idmac_desc_64addr) *
519 (i + 1))) >> 32;
520 /* Initialize reserved and buffer size fields to "0" */
521 p->des1 = 0;
522 p->des2 = 0;
523 p->des3 = 0;
524 }
525
526 /* Set the last descriptor as the end-of-ring descriptor */
527 p->des6 = host->sg_dma & 0xffffffff;
528 p->des7 = (u64)host->sg_dma >> 32;
529 p->des0 = IDMAC_DES0_ER;
530
531 } else {
532 struct idmac_desc *p;
533 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800534 host->ring_size =
535 DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000536
537 /* Forward link the descriptor list */
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800538 for (i = 0, p = host->sg_cpu;
539 i < host->ring_size - 1;
540 i++, p++) {
Ben Dooks6687c422015-03-25 11:27:51 +0000541 p->des3 = cpu_to_le32(host->sg_dma +
542 (sizeof(struct idmac_desc) * (i + 1)));
Zhangfei Gao4b244722015-04-30 22:16:28 +0800543 p->des1 = 0;
544 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000545
546 /* Set the last descriptor as the end-of-ring descriptor */
Ben Dooks6687c422015-03-25 11:27:51 +0000547 p->des3 = cpu_to_le32(host->sg_dma);
548 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000549 }
Will Newtonf95f3852011-01-02 01:11:59 -0500550
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900551 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900552
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000553 if (host->dma_64bit_address == 1) {
554 /* Mask out interrupts - get Tx & Rx complete only */
555 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
556 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
557 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500558
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000559 /* Set the descriptor base address */
560 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
561 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
562
563 } else {
564 /* Mask out interrupts - get Tx & Rx complete only */
565 mci_writel(host, IDSTS, IDMAC_INT_CLR);
566 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
567 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
568
569 /* Set the descriptor base address */
570 mci_writel(host, DBADDR, host->sg_dma);
571 }
572
Will Newtonf95f3852011-01-02 01:11:59 -0500573 return 0;
574}
575
Shawn Lin3b2a0672016-09-02 12:14:37 +0800576static inline int dw_mci_prepare_desc64(struct dw_mci *host,
577 struct mmc_data *data,
578 unsigned int sg_len)
579{
580 unsigned int desc_len;
581 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
Shawn Linb6d2d812017-02-17 10:56:39 +0800582 u32 val;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800583 int i;
584
585 desc_first = desc_last = desc = host->sg_cpu;
586
587 for (i = 0; i < sg_len; i++) {
588 unsigned int length = sg_dma_len(&data->sg[i]);
589
590 u64 mem_addr = sg_dma_address(&data->sg[i]);
591
592 for ( ; length ; desc++) {
593 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
594 length : DW_MCI_DESC_DATA_LENGTH;
595
596 length -= desc_len;
597
598 /*
599 * Wait for the former clear OWN bit operation
600 * of IDMAC to make sure that this descriptor
601 * isn't still owned by IDMAC as IDMAC's write
602 * ops and CPU's read ops are asynchronous.
603 */
Shawn Linb6d2d812017-02-17 10:56:39 +0800604 if (readl_poll_timeout_atomic(&desc->des0, val,
605 !(val & IDMAC_DES0_OWN),
606 10, 100 * USEC_PER_MSEC))
607 goto err_own_bit;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800608
609 /*
610 * Set the OWN bit and disable interrupts
611 * for this descriptor
612 */
613 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
614 IDMAC_DES0_CH;
615
616 /* Buffer length */
617 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
618
619 /* Physical address to DMA to/from */
620 desc->des4 = mem_addr & 0xffffffff;
621 desc->des5 = mem_addr >> 32;
622
623 /* Update physical address for the next desc */
624 mem_addr += desc_len;
625
626 /* Save pointer to the last descriptor */
627 desc_last = desc;
628 }
629 }
630
631 /* Set first descriptor */
632 desc_first->des0 |= IDMAC_DES0_FD;
633
634 /* Set last descriptor */
635 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
636 desc_last->des0 |= IDMAC_DES0_LD;
637
638 return 0;
639err_own_bit:
640 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000641 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800642 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800643 dw_mci_idmac_init(host);
644 return -EINVAL;
645}
646
647
648static inline int dw_mci_prepare_desc32(struct dw_mci *host,
649 struct mmc_data *data,
650 unsigned int sg_len)
651{
652 unsigned int desc_len;
653 struct idmac_desc *desc_first, *desc_last, *desc;
Shawn Linb6d2d812017-02-17 10:56:39 +0800654 u32 val;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800655 int i;
656
657 desc_first = desc_last = desc = host->sg_cpu;
658
659 for (i = 0; i < sg_len; i++) {
660 unsigned int length = sg_dma_len(&data->sg[i]);
661
662 u32 mem_addr = sg_dma_address(&data->sg[i]);
663
664 for ( ; length ; desc++) {
665 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
666 length : DW_MCI_DESC_DATA_LENGTH;
667
668 length -= desc_len;
669
670 /*
671 * Wait for the former clear OWN bit operation
672 * of IDMAC to make sure that this descriptor
673 * isn't still owned by IDMAC as IDMAC's write
674 * ops and CPU's read ops are asynchronous.
675 */
Shawn Linb6d2d812017-02-17 10:56:39 +0800676 if (readl_poll_timeout_atomic(&desc->des0, val,
677 IDMAC_OWN_CLR64(val),
678 10,
679 100 * USEC_PER_MSEC))
680 goto err_own_bit;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800681
682 /*
683 * Set the OWN bit and disable interrupts
684 * for this descriptor
685 */
686 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
687 IDMAC_DES0_DIC |
688 IDMAC_DES0_CH);
689
690 /* Buffer length */
691 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
692
693 /* Physical address to DMA to/from */
694 desc->des2 = cpu_to_le32(mem_addr);
695
696 /* Update physical address for the next desc */
697 mem_addr += desc_len;
698
699 /* Save pointer to the last descriptor */
700 desc_last = desc;
701 }
702 }
703
704 /* Set first descriptor */
705 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
706
707 /* Set last descriptor */
708 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
709 IDMAC_DES0_DIC));
710 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
711
712 return 0;
713err_own_bit:
714 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000715 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800716 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800717 dw_mci_idmac_init(host);
718 return -EINVAL;
719}
720
721static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
722{
723 u32 temp;
724 int ret;
725
726 if (host->dma_64bit_address == 1)
727 ret = dw_mci_prepare_desc64(host, host->data, sg_len);
728 else
729 ret = dw_mci_prepare_desc32(host, host->data, sg_len);
730
731 if (ret)
732 goto out;
733
734 /* drain writebuffer */
735 wmb();
736
737 /* Make sure to reset DMA in case we did PIO before this */
738 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
739 dw_mci_idmac_reset(host);
740
741 /* Select IDMAC interface */
742 temp = mci_readl(host, CTRL);
743 temp |= SDMMC_CTRL_USE_IDMAC;
744 mci_writel(host, CTRL, temp);
745
746 /* drain writebuffer */
747 wmb();
748
749 /* Enable the IDMAC */
750 temp = mci_readl(host, BMOD);
751 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
752 mci_writel(host, BMOD, temp);
753
754 /* Start it running */
755 mci_writel(host, PLDMND, 1);
756
757out:
758 return ret;
759}
760
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100761static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900762 .init = dw_mci_idmac_init,
763 .start = dw_mci_idmac_start_dma,
764 .stop = dw_mci_idmac_stop_dma,
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800765 .complete = dw_mci_dmac_complete_dma,
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900766 .cleanup = dw_mci_dma_cleanup,
767};
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800768
769static void dw_mci_edmac_stop_dma(struct dw_mci *host)
770{
Shawn Linab925a32016-03-09 10:34:46 +0800771 dmaengine_terminate_async(host->dms->ch);
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800772}
773
774static int dw_mci_edmac_start_dma(struct dw_mci *host,
775 unsigned int sg_len)
776{
777 struct dma_slave_config cfg;
778 struct dma_async_tx_descriptor *desc = NULL;
779 struct scatterlist *sgl = host->data->sg;
780 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
781 u32 sg_elems = host->data->sg_len;
782 u32 fifoth_val;
783 u32 fifo_offset = host->fifo_reg - host->regs;
784 int ret = 0;
785
786 /* Set external dma config: burst size, burst width */
Arnd Bergmann260b3162015-11-12 15:14:23 +0100787 cfg.dst_addr = host->phy_regs + fifo_offset;
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800788 cfg.src_addr = cfg.dst_addr;
789 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
790 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
791
792 /* Match burst msize with external dma config */
793 fifoth_val = mci_readl(host, FIFOTH);
794 cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
795 cfg.src_maxburst = cfg.dst_maxburst;
796
797 if (host->data->flags & MMC_DATA_WRITE)
798 cfg.direction = DMA_MEM_TO_DEV;
799 else
800 cfg.direction = DMA_DEV_TO_MEM;
801
802 ret = dmaengine_slave_config(host->dms->ch, &cfg);
803 if (ret) {
804 dev_err(host->dev, "Failed to config edmac.\n");
805 return -EBUSY;
806 }
807
808 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
809 sg_len, cfg.direction,
810 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
811 if (!desc) {
812 dev_err(host->dev, "Can't prepare slave sg.\n");
813 return -EBUSY;
814 }
815
816 /* Set dw_mci_dmac_complete_dma as callback */
817 desc->callback = dw_mci_dmac_complete_dma;
818 desc->callback_param = (void *)host;
819 dmaengine_submit(desc);
820
821 /* Flush cache before write */
822 if (host->data->flags & MMC_DATA_WRITE)
823 dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
824 sg_elems, DMA_TO_DEVICE);
825
826 dma_async_issue_pending(host->dms->ch);
827
828 return 0;
829}
830
831static int dw_mci_edmac_init(struct dw_mci *host)
832{
833 /* Request external dma channel */
834 host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
835 if (!host->dms)
836 return -ENOMEM;
837
838 host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
839 if (!host->dms->ch) {
Dan Carpenter4539d362015-10-22 22:53:46 +0300840 dev_err(host->dev, "Failed to get external DMA channel.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800841 kfree(host->dms);
842 host->dms = NULL;
843 return -ENXIO;
844 }
845
846 return 0;
847}
848
849static void dw_mci_edmac_exit(struct dw_mci *host)
850{
851 if (host->dms) {
852 if (host->dms->ch) {
853 dma_release_channel(host->dms->ch);
854 host->dms->ch = NULL;
855 }
856 kfree(host->dms);
857 host->dms = NULL;
858 }
859}
860
861static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
862 .init = dw_mci_edmac_init,
863 .exit = dw_mci_edmac_exit,
864 .start = dw_mci_edmac_start_dma,
865 .stop = dw_mci_edmac_stop_dma,
866 .complete = dw_mci_dmac_complete_dma,
867 .cleanup = dw_mci_dma_cleanup,
868};
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900869
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900870static int dw_mci_pre_dma_transfer(struct dw_mci *host,
871 struct mmc_data *data,
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900872 int cookie)
Will Newtonf95f3852011-01-02 01:11:59 -0500873{
874 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900875 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500876
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900877 if (data->host_cookie == COOKIE_PRE_MAPPED)
878 return data->sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500879
880 /*
881 * We don't do DMA on "complex" transfers, i.e. with
882 * non-word-aligned buffers or lengths. Also, we don't bother
883 * with all the DMA setup overhead for short transfers.
884 */
885 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
886 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900887
Will Newtonf95f3852011-01-02 01:11:59 -0500888 if (data->blksz & 3)
889 return -EINVAL;
890
891 for_each_sg(data->sg, sg, data->sg_len, i) {
892 if (sg->offset & 3 || sg->length & 3)
893 return -EINVAL;
894 }
895
Thomas Abraham4a909202012-09-17 18:16:35 +0000896 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900897 data->sg,
898 data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200899 mmc_get_dma_dir(data));
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900900 if (sg_len == 0)
901 return -EINVAL;
902
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900903 data->host_cookie = cookie;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900904
905 return sg_len;
906}
907
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900908static void dw_mci_pre_req(struct mmc_host *mmc,
Linus Walleijd3c6aac2016-11-23 11:02:24 +0100909 struct mmc_request *mrq)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900910{
911 struct dw_mci_slot *slot = mmc_priv(mmc);
912 struct mmc_data *data = mrq->data;
913
914 if (!slot->host->use_dma || !data)
915 return;
916
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900917 /* This data might be unmapped at this time */
918 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900919
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900920 if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
921 COOKIE_PRE_MAPPED) < 0)
922 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900923}
924
925static void dw_mci_post_req(struct mmc_host *mmc,
926 struct mmc_request *mrq,
927 int err)
928{
929 struct dw_mci_slot *slot = mmc_priv(mmc);
930 struct mmc_data *data = mrq->data;
931
932 if (!slot->host->use_dma || !data)
933 return;
934
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900935 if (data->host_cookie != COOKIE_UNMAPPED)
Thomas Abraham4a909202012-09-17 18:16:35 +0000936 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900937 data->sg,
938 data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200939 mmc_get_dma_dir(data));
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900940 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900941}
942
Shawn Lin671fa142017-02-17 10:56:42 +0800943static int dw_mci_get_cd(struct mmc_host *mmc)
944{
945 int present;
946 struct dw_mci_slot *slot = mmc_priv(mmc);
947 struct dw_mci *host = slot->host;
948 int gpio_cd = mmc_gpio_get_cd(mmc);
949
950 /* Use platform get_cd function, else try onboard card detect */
951 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
952 || !mmc_card_is_removable(mmc))) {
953 present = 1;
954
955 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
956 if (mmc->caps & MMC_CAP_NEEDS_POLL) {
957 dev_info(&mmc->class_dev,
958 "card is polling.\n");
959 } else {
960 dev_info(&mmc->class_dev,
961 "card is non-removable.\n");
962 }
963 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
964 }
965
966 return present;
967 } else if (gpio_cd >= 0)
968 present = gpio_cd;
969 else
970 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
971 == 0 ? 1 : 0;
972
973 spin_lock_bh(&host->lock);
974 if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
975 dev_dbg(&mmc->class_dev, "card is present\n");
976 else if (!present &&
977 !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
978 dev_dbg(&mmc->class_dev, "card is not present\n");
979 spin_unlock_bh(&host->lock);
980
981 return present;
982}
983
Seungwon Jeon524268992013-08-31 00:13:42 +0900984static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
985{
Seungwon Jeon524268992013-08-31 00:13:42 +0900986 unsigned int blksz = data->blksz;
987 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
988 u32 fifo_width = 1 << host->data_shift;
989 u32 blksz_depth = blksz / fifo_width, fifoth_val;
990 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800991 int idx = ARRAY_SIZE(mszs) - 1;
Seungwon Jeon524268992013-08-31 00:13:42 +0900992
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800993 /* pio should ship this scenario */
994 if (!host->use_dma)
995 return;
996
Seungwon Jeon524268992013-08-31 00:13:42 +0900997 tx_wmark = (host->fifo_depth) / 2;
998 tx_wmark_invers = host->fifo_depth - tx_wmark;
999
1000 /*
1001 * MSIZE is '1',
1002 * if blksz is not a multiple of the FIFO width
1003 */
Shawn Lin20753562016-09-21 10:40:25 +08001004 if (blksz % fifo_width)
Seungwon Jeon524268992013-08-31 00:13:42 +09001005 goto done;
Seungwon Jeon524268992013-08-31 00:13:42 +09001006
1007 do {
1008 if (!((blksz_depth % mszs[idx]) ||
1009 (tx_wmark_invers % mszs[idx]))) {
1010 msize = idx;
1011 rx_wmark = mszs[idx] - 1;
1012 break;
1013 }
1014 } while (--idx > 0);
1015 /*
1016 * If idx is '0', it won't be tried
1017 * Thus, initial values are uesed
1018 */
1019done:
1020 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
1021 mci_writel(host, FIFOTH, fifoth_val);
Seungwon Jeon524268992013-08-31 00:13:42 +09001022}
1023
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001024static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001025{
1026 unsigned int blksz = data->blksz;
1027 u32 blksz_depth, fifo_depth;
1028 u16 thld_size;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001029 u8 enable;
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001030
James Hogan66dfd102014-11-17 17:49:05 +00001031 /*
1032 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
1033 * in the FIFO region, so we really shouldn't access it).
1034 */
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001035 if (host->verid < DW_MMC_240A ||
1036 (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
James Hogan66dfd102014-11-17 17:49:05 +00001037 return;
1038
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001039 /*
1040 * Card write Threshold is introduced since 2.80a
1041 * It's used when HS400 mode is enabled.
1042 */
1043 if (data->flags & MMC_DATA_WRITE &&
1044 !(host->timing != MMC_TIMING_MMC_HS400))
1045 return;
1046
1047 if (data->flags & MMC_DATA_WRITE)
1048 enable = SDMMC_CARD_WR_THR_EN;
1049 else
1050 enable = SDMMC_CARD_RD_THR_EN;
1051
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001052 if (host->timing != MMC_TIMING_MMC_HS200 &&
1053 host->timing != MMC_TIMING_UHS_SDR104)
1054 goto disable;
1055
1056 blksz_depth = blksz / (1 << host->data_shift);
1057 fifo_depth = host->fifo_depth;
1058
1059 if (blksz_depth > fifo_depth)
1060 goto disable;
1061
1062 /*
1063 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1064 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
1065 * Currently just choose blksz.
1066 */
1067 thld_size = blksz;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001068 mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001069 return;
1070
1071disable:
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001072 mci_writel(host, CDTHRCTL, 0);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001073}
1074
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001075static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
1076{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001077 unsigned long irqflags;
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001078 int sg_len;
1079 u32 temp;
1080
1081 host->using_dma = 0;
1082
1083 /* If we don't have a channel, we can't do DMA */
1084 if (!host->use_dma)
1085 return -ENODEV;
1086
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +09001087 sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001088 if (sg_len < 0) {
1089 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001090 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001091 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001092
James Hogan03e8cb52011-06-29 09:28:43 +01001093 host->using_dma = 1;
1094
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001095 if (host->use_dma == TRANS_MODE_IDMAC)
1096 dev_vdbg(host->dev,
1097 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1098 (unsigned long)host->sg_cpu,
1099 (unsigned long)host->sg_dma,
1100 sg_len);
Will Newtonf95f3852011-01-02 01:11:59 -05001101
Seungwon Jeon524268992013-08-31 00:13:42 +09001102 /*
1103 * Decide the MSIZE and RX/TX Watermark.
1104 * If current block size is same with previous size,
1105 * no need to update fifoth.
1106 */
1107 if (host->prev_blksz != data->blksz)
1108 dw_mci_adjust_fifoth(host, data);
1109
Will Newtonf95f3852011-01-02 01:11:59 -05001110 /* Enable the DMA interface */
1111 temp = mci_readl(host, CTRL);
1112 temp |= SDMMC_CTRL_DMA_ENABLE;
1113 mci_writel(host, CTRL, temp);
1114
1115 /* Disable RX/TX IRQs, let DMA handle it */
Doug Andersonf8c58c12014-12-02 15:42:47 -08001116 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001117 temp = mci_readl(host, INTMASK);
1118 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1119 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001120 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001121
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001122 if (host->dma_ops->start(host, sg_len)) {
Jaehoon Chung647f80a2016-11-21 10:51:48 +09001123 host->dma_ops->stop(host);
Shawn Lind12d0cb2016-09-02 12:14:38 +08001124 /* We can't do DMA, try PIO for this one */
1125 dev_dbg(host->dev,
1126 "%s: fall back to PIO mode for current transfer\n",
1127 __func__);
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001128 return -ENODEV;
1129 }
Will Newtonf95f3852011-01-02 01:11:59 -05001130
1131 return 0;
1132}
1133
1134static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1135{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001136 unsigned long irqflags;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001137 int flags = SG_MITER_ATOMIC;
Will Newtonf95f3852011-01-02 01:11:59 -05001138 u32 temp;
1139
1140 data->error = -EINPROGRESS;
1141
1142 WARN_ON(host->data);
1143 host->sg = NULL;
1144 host->data = data;
1145
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001146 if (data->flags & MMC_DATA_READ)
James Hogan55c5efbc2011-06-29 09:29:58 +01001147 host->dir_status = DW_MCI_RECV_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001148 else
James Hogan55c5efbc2011-06-29 09:29:58 +01001149 host->dir_status = DW_MCI_SEND_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001150
1151 dw_mci_ctrl_thld(host, data);
James Hogan55c5efbc2011-06-29 09:29:58 +01001152
Will Newtonf95f3852011-01-02 01:11:59 -05001153 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001154 if (host->data->flags & MMC_DATA_READ)
1155 flags |= SG_MITER_TO_SG;
1156 else
1157 flags |= SG_MITER_FROM_SG;
1158
1159 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001160 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +01001161 host->part_buf_start = 0;
1162 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001163
James Hoganb40af3a2011-06-24 13:54:06 +01001164 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001165
1166 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001167 temp = mci_readl(host, INTMASK);
1168 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1169 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001170 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001171
1172 temp = mci_readl(host, CTRL);
1173 temp &= ~SDMMC_CTRL_DMA_ENABLE;
1174 mci_writel(host, CTRL, temp);
Seungwon Jeon524268992013-08-31 00:13:42 +09001175
1176 /*
Jun Nied6fced82017-01-11 15:37:26 +09001177 * Use the initial fifoth_val for PIO mode. If wm_algined
1178 * is set, we set watermark same as data size.
Seungwon Jeon524268992013-08-31 00:13:42 +09001179 * If next issued data may be transfered by DMA mode,
1180 * prev_blksz should be invalidated.
1181 */
Jun Nied6fced82017-01-11 15:37:26 +09001182 if (host->wm_aligned)
1183 dw_mci_adjust_fifoth(host, data);
1184 else
1185 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon524268992013-08-31 00:13:42 +09001186 host->prev_blksz = 0;
1187 } else {
1188 /*
1189 * Keep the current block size.
1190 * It will be used to decide whether to update
1191 * fifoth register next time.
1192 */
1193 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -05001194 }
1195}
1196
Abhilash Kesavanab269122012-11-19 10:26:21 +05301197static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -05001198{
1199 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +09001200 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001201 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001202 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +05301203 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1204
1205 /* We must continue to set bit 28 in CMD until the change is complete */
1206 if (host->state == STATE_WAITING_CMD11_DONE)
1207 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -05001208
Doug Andersonfdf492a2013-08-31 00:11:43 +09001209 if (!clock) {
1210 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +05301211 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +09001212 } else if (clock != host->current_speed || force_clkinit) {
1213 div = host->bus_hz / clock;
1214 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -05001215 /*
1216 * move the + 1 after the divide to prevent
1217 * over-clocking the card.
1218 */
Seungwon Jeone4199902012-05-22 13:01:21 +09001219 div += 1;
1220
Doug Andersonfdf492a2013-08-31 00:11:43 +09001221 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001222
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001223 if ((clock != slot->__clk_old &&
1224 !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1225 force_clkinit) {
Shawn Lince69e2f2017-01-17 09:22:55 +08001226 /* Silent the verbose log if calling from PM context */
1227 if (!force_clkinit)
1228 dev_info(&slot->mmc->class_dev,
1229 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1230 slot->id, host->bus_hz, clock,
1231 div ? ((host->bus_hz / div) >> 1) :
1232 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -05001233
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001234 /*
1235 * If card is polling, display the message only
1236 * one time at boot time.
1237 */
1238 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1239 slot->mmc->f_min == clock)
1240 set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1241 }
1242
Will Newtonf95f3852011-01-02 01:11:59 -05001243 /* disable clock */
1244 mci_writel(host, CLKENA, 0);
1245 mci_writel(host, CLKSRC, 0);
1246
1247 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301248 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001249
1250 /* set clock to desired speed */
1251 mci_writel(host, CLKDIV, div);
1252
1253 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301254 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001255
Doug Anderson9623b5b2012-07-25 08:33:17 -07001256 /* enable clock; only low power if no SDIO */
1257 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -08001258 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -07001259 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1260 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -05001261
1262 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301263 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Jaehoon Chung005d6752016-09-22 14:12:00 +09001264
1265 /* keep the last clock value that was requested from core */
1266 slot->__clk_old = clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001267 }
1268
Doug Andersonfdf492a2013-08-31 00:11:43 +09001269 host->current_speed = clock;
1270
Will Newtonf95f3852011-01-02 01:11:59 -05001271 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +09001272 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -05001273}
1274
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001275static void __dw_mci_start_request(struct dw_mci *host,
1276 struct dw_mci_slot *slot,
1277 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001278{
1279 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001280 struct mmc_data *data;
1281 u32 cmdflags;
1282
1283 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001284
Will Newtonf95f3852011-01-02 01:11:59 -05001285 host->cur_slot = slot;
1286 host->mrq = mrq;
1287
1288 host->pending_events = 0;
1289 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001290 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001291 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001292 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001293
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001294 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -05001295 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +09001296 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -05001297 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1298 mci_writel(host, BLKSIZ, data->blksz);
1299 }
1300
Will Newtonf95f3852011-01-02 01:11:59 -05001301 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1302
1303 /* this is the first command, send the initialization clock */
1304 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1305 cmdflags |= SDMMC_CMD_INIT;
1306
1307 if (data) {
1308 dw_mci_submit_data(host, data);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001309 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05001310 }
1311
1312 dw_mci_start_command(host, cmd, cmdflags);
1313
Doug Anderson5c935162015-03-09 16:18:21 -07001314 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
Doug Anderson49ba0302015-04-03 11:13:07 -07001315 unsigned long irqflags;
1316
Doug Anderson5c935162015-03-09 16:18:21 -07001317 /*
Doug Anderson8886a6f2015-04-03 11:13:05 -07001318 * Databook says to fail after 2ms w/ no response, but evidence
1319 * shows that sometimes the cmd11 interrupt takes over 130ms.
1320 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1321 * is just about to roll over.
Doug Anderson49ba0302015-04-03 11:13:07 -07001322 *
1323 * We do this whole thing under spinlock and only if the
1324 * command hasn't already completed (indicating the the irq
1325 * already ran so we don't want the timeout).
Doug Anderson5c935162015-03-09 16:18:21 -07001326 */
Doug Anderson49ba0302015-04-03 11:13:07 -07001327 spin_lock_irqsave(&host->irq_lock, irqflags);
1328 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1329 mod_timer(&host->cmd11_timer,
1330 jiffies + msecs_to_jiffies(500) + 1);
1331 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Doug Anderson5c935162015-03-09 16:18:21 -07001332 }
1333
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001334 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001335}
1336
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001337static void dw_mci_start_request(struct dw_mci *host,
1338 struct dw_mci_slot *slot)
1339{
1340 struct mmc_request *mrq = slot->mrq;
1341 struct mmc_command *cmd;
1342
1343 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1344 __dw_mci_start_request(host, slot, cmd);
1345}
1346
James Hogan7456caa2011-06-24 13:55:10 +01001347/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001348static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1349 struct mmc_request *mrq)
1350{
1351 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1352 host->state);
1353
Will Newtonf95f3852011-01-02 01:11:59 -05001354 slot->mrq = mrq;
1355
Doug Anderson01730552014-08-22 19:17:51 +05301356 if (host->state == STATE_WAITING_CMD11_DONE) {
1357 dev_warn(&slot->mmc->class_dev,
1358 "Voltage change didn't complete\n");
1359 /*
1360 * this case isn't expected to happen, so we can
1361 * either crash here or just try to continue on
1362 * in the closest possible state
1363 */
1364 host->state = STATE_IDLE;
1365 }
1366
Will Newtonf95f3852011-01-02 01:11:59 -05001367 if (host->state == STATE_IDLE) {
1368 host->state = STATE_SENDING_CMD;
1369 dw_mci_start_request(host, slot);
1370 } else {
1371 list_add_tail(&slot->queue_node, &host->queue);
1372 }
Will Newtonf95f3852011-01-02 01:11:59 -05001373}
1374
1375static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1376{
1377 struct dw_mci_slot *slot = mmc_priv(mmc);
1378 struct dw_mci *host = slot->host;
1379
1380 WARN_ON(slot->mrq);
1381
James Hogan7456caa2011-06-24 13:55:10 +01001382 /*
1383 * The check for card presence and queueing of the request must be
1384 * atomic, otherwise the card could be removed in between and the
1385 * request wouldn't fail until another card was inserted.
1386 */
James Hogan7456caa2011-06-24 13:55:10 +01001387
Shawn Lin56f69112016-05-27 14:37:05 +08001388 if (!dw_mci_get_cd(mmc)) {
Will Newtonf95f3852011-01-02 01:11:59 -05001389 mrq->cmd->error = -ENOMEDIUM;
1390 mmc_request_done(mmc, mrq);
1391 return;
1392 }
1393
Shawn Lin56f69112016-05-27 14:37:05 +08001394 spin_lock_bh(&host->lock);
1395
Will Newtonf95f3852011-01-02 01:11:59 -05001396 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001397
1398 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001399}
1400
1401static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1402{
1403 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001404 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001405 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301406 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001407
Will Newtonf95f3852011-01-02 01:11:59 -05001408 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001409 case MMC_BUS_WIDTH_4:
1410 slot->ctype = SDMMC_CTYPE_4BIT;
1411 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001412 case MMC_BUS_WIDTH_8:
1413 slot->ctype = SDMMC_CTYPE_8BIT;
1414 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001415 default:
1416 /* set default 1 bit mode */
1417 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001418 }
1419
Seungwon Jeon3f514292012-01-02 16:00:02 +09001420 regs = mci_readl(slot->host, UHS_REG);
1421
Jaehoon Chung41babf72011-02-24 13:46:11 +09001422 /* DDR mode set */
Seungwon Jeon80113132015-01-29 08:11:57 +05301423 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
Jaehoon Chung7cc8d582015-10-21 19:49:42 +09001424 ios->timing == MMC_TIMING_UHS_DDR50 ||
Seungwon Jeon80113132015-01-29 08:11:57 +05301425 ios->timing == MMC_TIMING_MMC_HS400)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001426 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001427 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001428 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001429
1430 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001431 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001432
Doug Andersonfdf492a2013-08-31 00:11:43 +09001433 /*
1434 * Use mirror of ios->clock to prevent race with mmc
1435 * core ios update when finding the minimum.
1436 */
1437 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001438
James Hogancb27a842012-10-16 09:43:08 +01001439 if (drv_data && drv_data->set_ios)
1440 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001441
Will Newtonf95f3852011-01-02 01:11:59 -05001442 switch (ios->power_mode) {
1443 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301444 if (!IS_ERR(mmc->supply.vmmc)) {
1445 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1446 ios->vdd);
1447 if (ret) {
1448 dev_err(slot->host->dev,
1449 "failed to enable vmmc regulator\n");
1450 /*return, if failed turn on vmmc*/
1451 return;
1452 }
1453 }
Doug Anderson29d0d162015-01-13 15:58:44 -08001454 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1455 regs = mci_readl(slot->host, PWREN);
1456 regs |= (1 << slot->id);
1457 mci_writel(slot->host, PWREN, regs);
1458 break;
1459 case MMC_POWER_ON:
Doug Andersond1f1dd82015-02-20 10:57:19 -08001460 if (!slot->host->vqmmc_enabled) {
1461 if (!IS_ERR(mmc->supply.vqmmc)) {
1462 ret = regulator_enable(mmc->supply.vqmmc);
1463 if (ret < 0)
1464 dev_err(slot->host->dev,
1465 "failed to enable vqmmc\n");
1466 else
1467 slot->host->vqmmc_enabled = true;
1468
1469 } else {
1470 /* Keep track so we don't reset again */
Yuvaraj CD51da2242014-08-22 19:17:50 +05301471 slot->host->vqmmc_enabled = true;
Doug Andersond1f1dd82015-02-20 10:57:19 -08001472 }
1473
1474 /* Reset our state machine after powering on */
1475 dw_mci_ctrl_reset(slot->host,
1476 SDMMC_CTRL_ALL_RESET_FLAGS);
Yuvaraj CD51da2242014-08-22 19:17:50 +05301477 }
Doug Anderson655babb2015-02-20 10:57:18 -08001478
1479 /* Adjust clock / bus width after power is up */
1480 dw_mci_setup_bus(slot, false);
1481
James Hogane6f34e22013-03-12 10:43:32 +00001482 break;
1483 case MMC_POWER_OFF:
Doug Anderson655babb2015-02-20 10:57:18 -08001484 /* Turn clock off before power goes down */
1485 dw_mci_setup_bus(slot, false);
1486
Yuvaraj CD51da2242014-08-22 19:17:50 +05301487 if (!IS_ERR(mmc->supply.vmmc))
1488 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1489
Doug Andersond1f1dd82015-02-20 10:57:19 -08001490 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
Yuvaraj CD51da2242014-08-22 19:17:50 +05301491 regulator_disable(mmc->supply.vqmmc);
Doug Andersond1f1dd82015-02-20 10:57:19 -08001492 slot->host->vqmmc_enabled = false;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301493
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001494 regs = mci_readl(slot->host, PWREN);
1495 regs &= ~(1 << slot->id);
1496 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001497 break;
1498 default:
1499 break;
1500 }
Doug Anderson655babb2015-02-20 10:57:18 -08001501
1502 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1503 slot->host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001504}
1505
Doug Anderson01730552014-08-22 19:17:51 +05301506static int dw_mci_card_busy(struct mmc_host *mmc)
1507{
1508 struct dw_mci_slot *slot = mmc_priv(mmc);
1509 u32 status;
1510
1511 /*
1512 * Check the busy bit which is low when DAT[3:0]
1513 * (the data lines) are 0000
1514 */
1515 status = mci_readl(slot->host, STATUS);
1516
1517 return !!(status & SDMMC_STATUS_BUSY);
1518}
1519
1520static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1521{
1522 struct dw_mci_slot *slot = mmc_priv(mmc);
1523 struct dw_mci *host = slot->host;
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001524 const struct dw_mci_drv_data *drv_data = host->drv_data;
Doug Anderson01730552014-08-22 19:17:51 +05301525 u32 uhs;
1526 u32 v18 = SDMMC_UHS_18V << slot->id;
Doug Anderson01730552014-08-22 19:17:51 +05301527 int ret;
1528
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001529 if (drv_data && drv_data->switch_voltage)
1530 return drv_data->switch_voltage(mmc, ios);
1531
Doug Anderson01730552014-08-22 19:17:51 +05301532 /*
1533 * Program the voltage. Note that some instances of dw_mmc may use
1534 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1535 * does no harm but you need to set the regulator directly. Try both.
1536 */
1537 uhs = mci_readl(host, UHS_REG);
Douglas Andersone0848f52015-10-12 14:48:26 +02001538 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
Doug Anderson01730552014-08-22 19:17:51 +05301539 uhs &= ~v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001540 else
Doug Anderson01730552014-08-22 19:17:51 +05301541 uhs |= v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001542
Doug Anderson01730552014-08-22 19:17:51 +05301543 if (!IS_ERR(mmc->supply.vqmmc)) {
Douglas Andersone0848f52015-10-12 14:48:26 +02001544 ret = mmc_regulator_set_vqmmc(mmc, ios);
Doug Anderson01730552014-08-22 19:17:51 +05301545
1546 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001547 dev_dbg(&mmc->class_dev,
Douglas Andersone0848f52015-10-12 14:48:26 +02001548 "Regulator set error %d - %s V\n",
1549 ret, uhs & v18 ? "1.8" : "3.3");
Doug Anderson01730552014-08-22 19:17:51 +05301550 return ret;
1551 }
1552 }
1553 mci_writel(host, UHS_REG, uhs);
1554
1555 return 0;
1556}
1557
Will Newtonf95f3852011-01-02 01:11:59 -05001558static int dw_mci_get_ro(struct mmc_host *mmc)
1559{
1560 int read_only;
1561 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001562 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001563
1564 /* Use platform get_ro function, else try on board write protect */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001565 if (gpio_ro >= 0)
Jaehoon Chung9795a842014-03-03 11:36:46 +09001566 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001567 else
1568 read_only =
1569 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1570
1571 dev_dbg(&mmc->class_dev, "card is %s\n",
1572 read_only ? "read-only" : "read-write");
1573
1574 return read_only;
1575}
1576
Shawn Lin935a6652016-01-14 09:08:02 +08001577static void dw_mci_hw_reset(struct mmc_host *mmc)
1578{
1579 struct dw_mci_slot *slot = mmc_priv(mmc);
1580 struct dw_mci *host = slot->host;
1581 int reset;
1582
1583 if (host->use_dma == TRANS_MODE_IDMAC)
1584 dw_mci_idmac_reset(host);
1585
1586 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1587 SDMMC_CTRL_FIFO_RESET))
1588 return;
1589
1590 /*
1591 * According to eMMC spec, card reset procedure:
1592 * tRstW >= 1us: RST_n pulse width
1593 * tRSCA >= 200us: RST_n to Command time
1594 * tRSTH >= 1us: RST_n high period
1595 */
1596 reset = mci_readl(host, RST_N);
1597 reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1598 mci_writel(host, RST_N, reset);
1599 usleep_range(1, 2);
1600 reset |= SDMMC_RST_HWACTIVE << slot->id;
1601 mci_writel(host, RST_N, reset);
1602 usleep_range(200, 300);
1603}
1604
Doug Andersonb24c8b22014-12-02 15:42:46 -08001605static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001606{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001607 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001608 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001609
Doug Andersonb24c8b22014-12-02 15:42:46 -08001610 /*
1611 * Low power mode will stop the card clock when idle. According to the
1612 * description of the CLKENA register we should disable low power mode
1613 * for SDIO cards if we need SDIO interrupts to work.
1614 */
1615 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1616 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1617 u32 clk_en_a_old;
1618 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001619
Doug Andersonb24c8b22014-12-02 15:42:46 -08001620 clk_en_a_old = mci_readl(host, CLKENA);
1621
1622 if (card->type == MMC_TYPE_SDIO ||
1623 card->type == MMC_TYPE_SD_COMBO) {
Ulf Hansson0eebf9b92017-04-19 22:41:43 +02001624 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001625 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1626 } else {
Ulf Hansson0eebf9b92017-04-19 22:41:43 +02001627 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001628 clk_en_a = clk_en_a_old | clken_low_pwr;
1629 }
1630
1631 if (clk_en_a != clk_en_a_old) {
1632 mci_writel(host, CLKENA, clk_en_a);
1633 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1634 SDMMC_CMD_PRV_DAT_WAIT, 0);
1635 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001636 }
1637}
1638
Ulf Hansson32dba732017-04-18 13:29:20 +02001639static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301640{
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301641 struct dw_mci *host = slot->host;
Doug Andersonf8c58c12014-12-02 15:42:47 -08001642 unsigned long irqflags;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301643 u32 int_mask;
1644
Doug Andersonf8c58c12014-12-02 15:42:47 -08001645 spin_lock_irqsave(&host->irq_lock, irqflags);
1646
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301647 /* Enable/disable Slot Specific SDIO interrupt */
1648 int_mask = mci_readl(host, INTMASK);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001649 if (enb)
1650 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1651 else
1652 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1653 mci_writel(host, INTMASK, int_mask);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001654
1655 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301656}
1657
Ulf Hansson32dba732017-04-18 13:29:20 +02001658static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1659{
1660 struct dw_mci_slot *slot = mmc_priv(mmc);
Ulf Hanssonca8971c2017-04-18 13:37:32 +02001661 struct dw_mci *host = slot->host;
Ulf Hansson32dba732017-04-18 13:29:20 +02001662
1663 __dw_mci_enable_sdio_irq(slot, enb);
Ulf Hanssonca8971c2017-04-18 13:37:32 +02001664
1665 /* Avoid runtime suspending the device when SDIO IRQ is enabled */
1666 if (enb)
1667 pm_runtime_get_noresume(host->dev);
1668 else
1669 pm_runtime_put_noidle(host->dev);
Ulf Hansson32dba732017-04-18 13:29:20 +02001670}
1671
1672static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
1673{
1674 struct dw_mci_slot *slot = mmc_priv(mmc);
1675
1676 __dw_mci_enable_sdio_irq(slot, 1);
1677}
1678
Seungwon Jeon0976f162013-08-31 00:12:42 +09001679static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1680{
1681 struct dw_mci_slot *slot = mmc_priv(mmc);
1682 struct dw_mci *host = slot->host;
1683 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001684 int err = -EINVAL;
Seungwon Jeon0976f162013-08-31 00:12:42 +09001685
Seungwon Jeon0976f162013-08-31 00:12:42 +09001686 if (drv_data && drv_data->execute_tuning)
Chaotian Jing9979dbe2015-10-27 14:24:28 +08001687 err = drv_data->execute_tuning(slot, opcode);
Seungwon Jeon0976f162013-08-31 00:12:42 +09001688 return err;
1689}
1690
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001691static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1692 struct mmc_ios *ios)
Seungwon Jeon80113132015-01-29 08:11:57 +05301693{
1694 struct dw_mci_slot *slot = mmc_priv(mmc);
1695 struct dw_mci *host = slot->host;
1696 const struct dw_mci_drv_data *drv_data = host->drv_data;
1697
1698 if (drv_data && drv_data->prepare_hs400_tuning)
1699 return drv_data->prepare_hs400_tuning(host, ios);
1700
1701 return 0;
1702}
1703
Shawn Lin4e7392b2017-02-17 10:56:40 +08001704static bool dw_mci_reset(struct dw_mci *host)
1705{
1706 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
1707 bool ret = false;
Shawn Linbc2dcc12017-02-17 10:59:52 +08001708 u32 status = 0;
Shawn Lin4e7392b2017-02-17 10:56:40 +08001709
1710 /*
1711 * Resetting generates a block interrupt, hence setting
1712 * the scatter-gather pointer to NULL.
1713 */
1714 if (host->sg) {
1715 sg_miter_stop(&host->sg_miter);
1716 host->sg = NULL;
1717 }
1718
1719 if (host->use_dma)
1720 flags |= SDMMC_CTRL_DMA_RESET;
1721
1722 if (dw_mci_ctrl_reset(host, flags)) {
1723 /*
Shawn Linbc2dcc12017-02-17 10:59:52 +08001724 * In all cases we clear the RAWINTS
1725 * register to clear any interrupts.
Shawn Lin4e7392b2017-02-17 10:56:40 +08001726 */
1727 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1728
Shawn Linbc2dcc12017-02-17 10:59:52 +08001729 if (!host->use_dma) {
1730 ret = true;
1731 goto ciu_out;
Shawn Lin4e7392b2017-02-17 10:56:40 +08001732 }
Shawn Linbc2dcc12017-02-17 10:59:52 +08001733
1734 /* Wait for dma_req to be cleared */
1735 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
1736 status,
1737 !(status & SDMMC_STATUS_DMA_REQ),
1738 1, 500 * USEC_PER_MSEC)) {
1739 dev_err(host->dev,
1740 "%s: Timeout waiting for dma_req to be cleared\n",
1741 __func__);
1742 goto ciu_out;
1743 }
1744
1745 /* when using DMA next we reset the fifo again */
1746 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
1747 goto ciu_out;
Shawn Lin4e7392b2017-02-17 10:56:40 +08001748 } else {
1749 /* if the controller reset bit did clear, then set clock regs */
1750 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
1751 dev_err(host->dev,
1752 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
1753 __func__);
1754 goto ciu_out;
1755 }
1756 }
1757
1758 if (host->use_dma == TRANS_MODE_IDMAC)
1759 /* It is also recommended that we reset and reprogram idmac */
1760 dw_mci_idmac_reset(host);
1761
1762 ret = true;
1763
1764ciu_out:
1765 /* After a CTRL reset we need to have CIU set clock registers */
1766 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
1767
1768 return ret;
1769}
1770
Will Newtonf95f3852011-01-02 01:11:59 -05001771static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301772 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001773 .pre_req = dw_mci_pre_req,
1774 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301775 .set_ios = dw_mci_set_ios,
1776 .get_ro = dw_mci_get_ro,
1777 .get_cd = dw_mci_get_cd,
Shawn Lin935a6652016-01-14 09:08:02 +08001778 .hw_reset = dw_mci_hw_reset,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301779 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Ulf Hansson32dba732017-04-18 13:29:20 +02001780 .ack_sdio_irq = dw_mci_ack_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001781 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301782 .card_busy = dw_mci_card_busy,
1783 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001784 .init_card = dw_mci_init_card,
Seungwon Jeon80113132015-01-29 08:11:57 +05301785 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
Will Newtonf95f3852011-01-02 01:11:59 -05001786};
1787
1788static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1789 __releases(&host->lock)
1790 __acquires(&host->lock)
1791{
1792 struct dw_mci_slot *slot;
1793 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1794
1795 WARN_ON(host->cmd || host->data);
1796
1797 host->cur_slot->mrq = NULL;
1798 host->mrq = NULL;
1799 if (!list_empty(&host->queue)) {
1800 slot = list_entry(host->queue.next,
1801 struct dw_mci_slot, queue_node);
1802 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001803 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001804 mmc_hostname(slot->mmc));
1805 host->state = STATE_SENDING_CMD;
1806 dw_mci_start_request(host, slot);
1807 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001808 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301809
1810 if (host->state == STATE_SENDING_CMD11)
1811 host->state = STATE_WAITING_CMD11_DONE;
1812 else
1813 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001814 }
1815
1816 spin_unlock(&host->lock);
1817 mmc_request_done(prev_mmc, mrq);
1818 spin_lock(&host->lock);
1819}
1820
Seungwon Jeone352c812013-08-31 00:14:17 +09001821static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001822{
1823 u32 status = host->cmd_status;
1824
1825 host->cmd_status = 0;
1826
1827 /* Read the response from the card (up to 16 bytes) */
1828 if (cmd->flags & MMC_RSP_PRESENT) {
1829 if (cmd->flags & MMC_RSP_136) {
1830 cmd->resp[3] = mci_readl(host, RESP0);
1831 cmd->resp[2] = mci_readl(host, RESP1);
1832 cmd->resp[1] = mci_readl(host, RESP2);
1833 cmd->resp[0] = mci_readl(host, RESP3);
1834 } else {
1835 cmd->resp[0] = mci_readl(host, RESP0);
1836 cmd->resp[1] = 0;
1837 cmd->resp[2] = 0;
1838 cmd->resp[3] = 0;
1839 }
1840 }
1841
1842 if (status & SDMMC_INT_RTO)
1843 cmd->error = -ETIMEDOUT;
1844 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1845 cmd->error = -EILSEQ;
1846 else if (status & SDMMC_INT_RESP_ERR)
1847 cmd->error = -EIO;
1848 else
1849 cmd->error = 0;
1850
Seungwon Jeone352c812013-08-31 00:14:17 +09001851 return cmd->error;
1852}
1853
1854static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1855{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001856 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001857
1858 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1859 if (status & SDMMC_INT_DRTO) {
1860 data->error = -ETIMEDOUT;
1861 } else if (status & SDMMC_INT_DCRC) {
1862 data->error = -EILSEQ;
1863 } else if (status & SDMMC_INT_EBE) {
1864 if (host->dir_status ==
1865 DW_MCI_SEND_STATUS) {
1866 /*
1867 * No data CRC status was returned.
1868 * The number of bytes transferred
1869 * will be exaggerated in PIO mode.
1870 */
1871 data->bytes_xfered = 0;
1872 data->error = -ETIMEDOUT;
1873 } else if (host->dir_status ==
1874 DW_MCI_RECV_STATUS) {
Shawn Line7a1dec2016-08-22 10:57:16 +08001875 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001876 }
1877 } else {
1878 /* SDMMC_INT_SBE is included */
Shawn Line7a1dec2016-08-22 10:57:16 +08001879 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001880 }
1881
Doug Andersone6cc0122014-04-22 16:51:21 -07001882 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001883
1884 /*
1885 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001886 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001887 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001888 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001889 } else {
1890 data->bytes_xfered = data->blocks * data->blksz;
1891 data->error = 0;
1892 }
1893
1894 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001895}
1896
Addy Ke57e10482015-08-11 01:27:18 +09001897static void dw_mci_set_drto(struct dw_mci *host)
1898{
1899 unsigned int drto_clks;
1900 unsigned int drto_ms;
1901
1902 drto_clks = mci_readl(host, TMOUT) >> 8;
1903 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1904
1905 /* add a bit spare time */
1906 drto_ms += 10;
1907
1908 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1909}
1910
Will Newtonf95f3852011-01-02 01:11:59 -05001911static void dw_mci_tasklet_func(unsigned long priv)
1912{
1913 struct dw_mci *host = (struct dw_mci *)priv;
1914 struct mmc_data *data;
1915 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001916 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001917 enum dw_mci_state state;
1918 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001919 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001920
1921 spin_lock(&host->lock);
1922
1923 state = host->state;
1924 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001925 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001926
1927 do {
1928 prev_state = state;
1929
1930 switch (state) {
1931 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301932 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001933 break;
1934
Doug Anderson01730552014-08-22 19:17:51 +05301935 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001936 case STATE_SENDING_CMD:
1937 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1938 &host->pending_events))
1939 break;
1940
1941 cmd = host->cmd;
1942 host->cmd = NULL;
1943 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001944 err = dw_mci_command_complete(host, cmd);
1945 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001946 prev_state = state = STATE_SENDING_CMD;
1947 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001948 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001949 goto unlock;
1950 }
1951
Seungwon Jeone352c812013-08-31 00:14:17 +09001952 if (cmd->data && err) {
Doug Anderson46d17952016-04-26 10:03:58 +02001953 /*
1954 * During UHS tuning sequence, sending the stop
1955 * command after the response CRC error would
1956 * throw the system into a confused state
1957 * causing all future tuning phases to report
1958 * failure.
1959 *
1960 * In such case controller will move into a data
1961 * transfer state after a response error or
1962 * response CRC error. Let's let that finish
1963 * before trying to send a stop, so we'll go to
1964 * STATE_SENDING_DATA.
1965 *
1966 * Although letting the data transfer take place
1967 * will waste a bit of time (we already know
1968 * the command was bad), it can't cause any
1969 * errors since it's possible it would have
1970 * taken place anyway if this tasklet got
1971 * delayed. Allowing the transfer to take place
1972 * avoids races and keeps things simple.
1973 */
1974 if ((err != -ETIMEDOUT) &&
1975 (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
1976 state = STATE_SENDING_DATA;
1977 continue;
1978 }
1979
Seungwon Jeon71abb132013-08-31 00:13:59 +09001980 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001981 send_stop_abort(host, data);
1982 state = STATE_SENDING_STOP;
1983 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001984 }
1985
Seungwon Jeone352c812013-08-31 00:14:17 +09001986 if (!cmd->data || err) {
1987 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001988 goto unlock;
1989 }
1990
1991 prev_state = state = STATE_SENDING_DATA;
1992 /* fall through */
1993
1994 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001995 /*
1996 * We could get a data error and never a transfer
1997 * complete so we'd better check for it here.
1998 *
1999 * Note that we don't really care if we also got a
2000 * transfer complete; stopping the DMA and sending an
2001 * abort won't hurt.
2002 */
Will Newtonf95f3852011-01-02 01:11:59 -05002003 if (test_and_clear_bit(EVENT_DATA_ERROR,
2004 &host->pending_events)) {
2005 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002006 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08002007 SDMMC_INT_EBE)))
2008 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05002009 state = STATE_DATA_ERROR;
2010 break;
2011 }
2012
2013 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09002014 &host->pending_events)) {
2015 /*
2016 * If all data-related interrupts don't come
2017 * within the given time in reading data state.
2018 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09002019 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09002020 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002021 break;
Addy Ke57e10482015-08-11 01:27:18 +09002022 }
Will Newtonf95f3852011-01-02 01:11:59 -05002023
2024 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07002025
2026 /*
2027 * Handle an EVENT_DATA_ERROR that might have shown up
2028 * before the transfer completed. This might not have
2029 * been caught by the check above because the interrupt
2030 * could have gone off between the previous check and
2031 * the check for transfer complete.
2032 *
2033 * Technically this ought not be needed assuming we
2034 * get a DATA_COMPLETE eventually (we'll notice the
2035 * error and end the request), but it shouldn't hurt.
2036 *
2037 * This has the advantage of sending the stop command.
2038 */
2039 if (test_and_clear_bit(EVENT_DATA_ERROR,
2040 &host->pending_events)) {
2041 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002042 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08002043 SDMMC_INT_EBE)))
2044 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07002045 state = STATE_DATA_ERROR;
2046 break;
2047 }
Will Newtonf95f3852011-01-02 01:11:59 -05002048 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07002049
Will Newtonf95f3852011-01-02 01:11:59 -05002050 /* fall through */
2051
2052 case STATE_DATA_BUSY:
2053 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09002054 &host->pending_events)) {
2055 /*
2056 * If data error interrupt comes but data over
2057 * interrupt doesn't come within the given time.
2058 * in reading data state.
2059 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09002060 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09002061 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002062 break;
Addy Ke57e10482015-08-11 01:27:18 +09002063 }
Will Newtonf95f3852011-01-02 01:11:59 -05002064
2065 host->data = NULL;
2066 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09002067 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05002068
Seungwon Jeone352c812013-08-31 00:14:17 +09002069 if (!err) {
2070 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05302071 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002072 data->stop->error = 0;
2073 dw_mci_request_end(host, mrq);
2074 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05002075 }
Will Newtonf95f3852011-01-02 01:11:59 -05002076
Seungwon Jeon90c21432013-08-31 00:14:05 +09002077 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09002078 if (data->stop)
2079 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07002080 } else {
2081 /*
2082 * If we don't have a command complete now we'll
2083 * never get one since we just reset everything;
2084 * better end the request.
2085 *
2086 * If we do have a command complete we'll fall
2087 * through to the SENDING_STOP command and
2088 * everything will be peachy keen.
2089 */
2090 if (!test_bit(EVENT_CMD_COMPLETE,
2091 &host->pending_events)) {
2092 host->cmd = NULL;
2093 dw_mci_request_end(host, mrq);
2094 goto unlock;
2095 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09002096 }
Seungwon Jeone352c812013-08-31 00:14:17 +09002097
2098 /*
2099 * If err has non-zero,
2100 * stop-abort command has been already issued.
2101 */
2102 prev_state = state = STATE_SENDING_STOP;
2103
Will Newtonf95f3852011-01-02 01:11:59 -05002104 /* fall through */
2105
2106 case STATE_SENDING_STOP:
2107 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2108 &host->pending_events))
2109 break;
2110
Seungwon Jeon71abb132013-08-31 00:13:59 +09002111 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09002112 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07002113 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09002114
Will Newtonf95f3852011-01-02 01:11:59 -05002115 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09002116 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09002117
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002118 if (!mrq->sbc && mrq->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002119 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09002120 else
2121 host->cmd_status = 0;
2122
Seungwon Jeone352c812013-08-31 00:14:17 +09002123 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05002124 goto unlock;
2125
2126 case STATE_DATA_ERROR:
2127 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2128 &host->pending_events))
2129 break;
2130
2131 state = STATE_DATA_BUSY;
2132 break;
2133 }
2134 } while (state != prev_state);
2135
2136 host->state = state;
2137unlock:
2138 spin_unlock(&host->lock);
2139
2140}
2141
James Hogan34b664a2011-06-24 13:57:56 +01002142/* push final bytes to part_buf, only use during push */
2143static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2144{
2145 memcpy((void *)&host->part_buf, buf, cnt);
2146 host->part_buf_count = cnt;
2147}
2148
2149/* append bytes to part_buf, only use during push */
2150static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2151{
2152 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2153 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2154 host->part_buf_count += cnt;
2155 return cnt;
2156}
2157
2158/* pull first bytes from part_buf, only use during pull */
2159static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2160{
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002161 cnt = min_t(int, cnt, host->part_buf_count);
James Hogan34b664a2011-06-24 13:57:56 +01002162 if (cnt) {
2163 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2164 cnt);
2165 host->part_buf_count -= cnt;
2166 host->part_buf_start += cnt;
2167 }
2168 return cnt;
2169}
2170
2171/* pull final bytes from the part_buf, assuming it's just been filled */
2172static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2173{
2174 memcpy(buf, &host->part_buf, cnt);
2175 host->part_buf_start = cnt;
2176 host->part_buf_count = (1 << host->data_shift) - cnt;
2177}
2178
Will Newtonf95f3852011-01-02 01:11:59 -05002179static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2180{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002181 struct mmc_data *data = host->data;
2182 int init_cnt = cnt;
2183
James Hogan34b664a2011-06-24 13:57:56 +01002184 /* try and push anything in the part_buf */
2185 if (unlikely(host->part_buf_count)) {
2186 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002187
James Hogan34b664a2011-06-24 13:57:56 +01002188 buf += len;
2189 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002190 if (host->part_buf_count == 2) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002191 mci_fifo_writew(host->fifo_reg, host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01002192 host->part_buf_count = 0;
2193 }
2194 }
2195#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2196 if (unlikely((unsigned long)buf & 0x1)) {
2197 while (cnt >= 2) {
2198 u16 aligned_buf[64];
2199 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2200 int items = len >> 1;
2201 int i;
2202 /* memcpy from input buffer into aligned buffer */
2203 memcpy(aligned_buf, buf, len);
2204 buf += len;
2205 cnt -= len;
2206 /* push data from aligned buffer into fifo */
2207 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002208 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002209 }
2210 } else
2211#endif
2212 {
2213 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002214
James Hogan34b664a2011-06-24 13:57:56 +01002215 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002216 mci_fifo_writew(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002217 buf = pdata;
2218 }
2219 /* put anything remaining in the part_buf */
2220 if (cnt) {
2221 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002222 /* Push data if we have reached the expected data length */
2223 if ((data->bytes_xfered + init_cnt) ==
2224 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002225 mci_fifo_writew(host->fifo_reg, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05002226 }
2227}
2228
2229static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2230{
James Hogan34b664a2011-06-24 13:57:56 +01002231#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2232 if (unlikely((unsigned long)buf & 0x1)) {
2233 while (cnt >= 2) {
2234 /* pull data from fifo into aligned buffer */
2235 u16 aligned_buf[64];
2236 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2237 int items = len >> 1;
2238 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002239
James Hogan34b664a2011-06-24 13:57:56 +01002240 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002241 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002242 /* memcpy from aligned buffer into output buffer */
2243 memcpy(buf, aligned_buf, len);
2244 buf += len;
2245 cnt -= len;
2246 }
2247 } else
2248#endif
2249 {
2250 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002251
James Hogan34b664a2011-06-24 13:57:56 +01002252 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002253 *pdata++ = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002254 buf = pdata;
2255 }
2256 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002257 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002258 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002259 }
2260}
2261
2262static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2263{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002264 struct mmc_data *data = host->data;
2265 int init_cnt = cnt;
2266
James Hogan34b664a2011-06-24 13:57:56 +01002267 /* try and push anything in the part_buf */
2268 if (unlikely(host->part_buf_count)) {
2269 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002270
James Hogan34b664a2011-06-24 13:57:56 +01002271 buf += len;
2272 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002273 if (host->part_buf_count == 4) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002274 mci_fifo_writel(host->fifo_reg, host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01002275 host->part_buf_count = 0;
2276 }
2277 }
2278#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2279 if (unlikely((unsigned long)buf & 0x3)) {
2280 while (cnt >= 4) {
2281 u32 aligned_buf[32];
2282 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2283 int items = len >> 2;
2284 int i;
2285 /* memcpy from input buffer into aligned buffer */
2286 memcpy(aligned_buf, buf, len);
2287 buf += len;
2288 cnt -= len;
2289 /* push data from aligned buffer into fifo */
2290 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002291 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002292 }
2293 } else
2294#endif
2295 {
2296 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002297
James Hogan34b664a2011-06-24 13:57:56 +01002298 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002299 mci_fifo_writel(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002300 buf = pdata;
2301 }
2302 /* put anything remaining in the part_buf */
2303 if (cnt) {
2304 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002305 /* Push data if we have reached the expected data length */
2306 if ((data->bytes_xfered + init_cnt) ==
2307 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002308 mci_fifo_writel(host->fifo_reg, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05002309 }
2310}
2311
2312static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2313{
James Hogan34b664a2011-06-24 13:57:56 +01002314#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2315 if (unlikely((unsigned long)buf & 0x3)) {
2316 while (cnt >= 4) {
2317 /* pull data from fifo into aligned buffer */
2318 u32 aligned_buf[32];
2319 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2320 int items = len >> 2;
2321 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002322
James Hogan34b664a2011-06-24 13:57:56 +01002323 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002324 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002325 /* memcpy from aligned buffer into output buffer */
2326 memcpy(buf, aligned_buf, len);
2327 buf += len;
2328 cnt -= len;
2329 }
2330 } else
2331#endif
2332 {
2333 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002334
James Hogan34b664a2011-06-24 13:57:56 +01002335 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002336 *pdata++ = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002337 buf = pdata;
2338 }
2339 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002340 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002341 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002342 }
2343}
2344
2345static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2346{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002347 struct mmc_data *data = host->data;
2348 int init_cnt = cnt;
2349
James Hogan34b664a2011-06-24 13:57:56 +01002350 /* try and push anything in the part_buf */
2351 if (unlikely(host->part_buf_count)) {
2352 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002353
James Hogan34b664a2011-06-24 13:57:56 +01002354 buf += len;
2355 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09002356
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002357 if (host->part_buf_count == 8) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002358 mci_fifo_writeq(host->fifo_reg, host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01002359 host->part_buf_count = 0;
2360 }
2361 }
2362#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2363 if (unlikely((unsigned long)buf & 0x7)) {
2364 while (cnt >= 8) {
2365 u64 aligned_buf[16];
2366 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2367 int items = len >> 3;
2368 int i;
2369 /* memcpy from input buffer into aligned buffer */
2370 memcpy(aligned_buf, buf, len);
2371 buf += len;
2372 cnt -= len;
2373 /* push data from aligned buffer into fifo */
2374 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002375 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002376 }
2377 } else
2378#endif
2379 {
2380 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002381
James Hogan34b664a2011-06-24 13:57:56 +01002382 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002383 mci_fifo_writeq(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002384 buf = pdata;
2385 }
2386 /* put anything remaining in the part_buf */
2387 if (cnt) {
2388 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002389 /* Push data if we have reached the expected data length */
2390 if ((data->bytes_xfered + init_cnt) ==
2391 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002392 mci_fifo_writeq(host->fifo_reg, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05002393 }
2394}
2395
2396static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2397{
James Hogan34b664a2011-06-24 13:57:56 +01002398#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2399 if (unlikely((unsigned long)buf & 0x7)) {
2400 while (cnt >= 8) {
2401 /* pull data from fifo into aligned buffer */
2402 u64 aligned_buf[16];
2403 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2404 int items = len >> 3;
2405 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002406
James Hogan34b664a2011-06-24 13:57:56 +01002407 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002408 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2409
James Hogan34b664a2011-06-24 13:57:56 +01002410 /* memcpy from aligned buffer into output buffer */
2411 memcpy(buf, aligned_buf, len);
2412 buf += len;
2413 cnt -= len;
2414 }
2415 } else
2416#endif
2417 {
2418 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002419
James Hogan34b664a2011-06-24 13:57:56 +01002420 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002421 *pdata++ = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002422 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05002423 }
James Hogan34b664a2011-06-24 13:57:56 +01002424 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002425 host->part_buf = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002426 dw_mci_pull_final_bytes(host, buf, cnt);
2427 }
2428}
2429
2430static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2431{
2432 int len;
2433
2434 /* get remaining partial bytes */
2435 len = dw_mci_pull_part_bytes(host, buf, cnt);
2436 if (unlikely(len == cnt))
2437 return;
2438 buf += len;
2439 cnt -= len;
2440
2441 /* get the rest of the data */
2442 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002443}
2444
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002445static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05002446{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002447 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2448 void *buf;
2449 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002450 struct mmc_data *data = host->data;
2451 int shift = host->data_shift;
2452 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002453 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002454 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002455
2456 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002457 if (!sg_miter_next(sg_miter))
2458 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002459
Imre Deak4225fc82013-02-27 17:02:57 -08002460 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002461 buf = sg_miter->addr;
2462 remain = sg_miter->length;
2463 offset = 0;
2464
2465 do {
2466 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2467 << shift) + host->part_buf_count;
2468 len = min(remain, fcnt);
2469 if (!len)
2470 break;
2471 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002472 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002473 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002474 remain -= len;
2475 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002476
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002477 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002478 status = mci_readl(host, MINTSTS);
2479 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002480 /* if the RXDR is ready read again */
2481 } while ((status & SDMMC_INT_RXDR) ||
2482 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002483
2484 if (!remain) {
2485 if (!sg_miter_next(sg_miter))
2486 goto done;
2487 sg_miter->consumed = 0;
2488 }
2489 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002490 return;
2491
2492done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002493 sg_miter_stop(sg_miter);
2494 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002495 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002496 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2497}
2498
2499static void dw_mci_write_data_pio(struct dw_mci *host)
2500{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002501 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2502 void *buf;
2503 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002504 struct mmc_data *data = host->data;
2505 int shift = host->data_shift;
2506 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002507 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002508 unsigned int fifo_depth = host->fifo_depth;
2509 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002510
2511 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002512 if (!sg_miter_next(sg_miter))
2513 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002514
Imre Deak4225fc82013-02-27 17:02:57 -08002515 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002516 buf = sg_miter->addr;
2517 remain = sg_miter->length;
2518 offset = 0;
2519
2520 do {
2521 fcnt = ((fifo_depth -
2522 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2523 << shift) - host->part_buf_count;
2524 len = min(remain, fcnt);
2525 if (!len)
2526 break;
2527 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002528 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002529 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002530 remain -= len;
2531 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002532
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002533 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002534 status = mci_readl(host, MINTSTS);
2535 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002536 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002537
2538 if (!remain) {
2539 if (!sg_miter_next(sg_miter))
2540 goto done;
2541 sg_miter->consumed = 0;
2542 }
2543 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002544 return;
2545
2546done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002547 sg_miter_stop(sg_miter);
2548 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002549 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002550 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2551}
2552
2553static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2554{
2555 if (!host->cmd_status)
2556 host->cmd_status = status;
2557
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002558 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002559
2560 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2561 tasklet_schedule(&host->tasklet);
2562}
2563
Doug Anderson6130e7a2014-10-14 09:33:09 -07002564static void dw_mci_handle_cd(struct dw_mci *host)
2565{
Jaehoon Chung58870242017-06-05 13:41:31 +09002566 int i = 0;
2567 struct dw_mci_slot *slot = host->slot[i];
Doug Anderson6130e7a2014-10-14 09:33:09 -07002568
Jaehoon Chung58870242017-06-05 13:41:31 +09002569 if (slot->mmc->ops->card_event)
2570 slot->mmc->ops->card_event(slot->mmc);
2571 mmc_detect_change(slot->mmc,
2572 msecs_to_jiffies(host->pdata->detect_delay_ms));
Doug Anderson6130e7a2014-10-14 09:33:09 -07002573}
2574
Will Newtonf95f3852011-01-02 01:11:59 -05002575static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2576{
2577 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002578 u32 pending;
Jaehoon Chung58870242017-06-05 13:41:31 +09002579 int i = 0;
2580 struct dw_mci_slot *slot = host->slot[i];
Will Newtonf95f3852011-01-02 01:11:59 -05002581
Markos Chandras1fb5f682013-03-12 10:53:11 +00002582 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2583
2584 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302585 /* Check volt switch first, since it can look like an error */
2586 if ((host->state == STATE_SENDING_CMD11) &&
2587 (pending & SDMMC_INT_VOLT_SWITCH)) {
Doug Anderson49ba0302015-04-03 11:13:07 -07002588 unsigned long irqflags;
Doug Anderson5c935162015-03-09 16:18:21 -07002589
Doug Anderson01730552014-08-22 19:17:51 +05302590 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2591 pending &= ~SDMMC_INT_VOLT_SWITCH;
Doug Anderson49ba0302015-04-03 11:13:07 -07002592
2593 /*
2594 * Hold the lock; we know cmd11_timer can't be kicked
2595 * off after the lock is released, so safe to delete.
2596 */
2597 spin_lock_irqsave(&host->irq_lock, irqflags);
Doug Anderson01730552014-08-22 19:17:51 +05302598 dw_mci_cmd_interrupt(host, pending);
Doug Anderson49ba0302015-04-03 11:13:07 -07002599 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2600
2601 del_timer(&host->cmd11_timer);
Doug Anderson01730552014-08-22 19:17:51 +05302602 }
2603
Will Newtonf95f3852011-01-02 01:11:59 -05002604 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2605 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002606 host->cmd_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002607 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002608 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002609 }
2610
2611 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2612 /* if there is an error report DATA_ERROR */
2613 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002614 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002615 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002616 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002617 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002618 }
2619
2620 if (pending & SDMMC_INT_DATA_OVER) {
Jaehoon Chung16a34572016-06-21 14:35:37 +09002621 del_timer(&host->dto_timer);
Addy Ke57e10482015-08-11 01:27:18 +09002622
Will Newtonf95f3852011-01-02 01:11:59 -05002623 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2624 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002625 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002626 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002627 if (host->dir_status == DW_MCI_RECV_STATUS) {
2628 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002629 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002630 }
2631 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2632 tasklet_schedule(&host->tasklet);
2633 }
2634
2635 if (pending & SDMMC_INT_RXDR) {
2636 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002637 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002638 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002639 }
2640
2641 if (pending & SDMMC_INT_TXDR) {
2642 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002643 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002644 dw_mci_write_data_pio(host);
2645 }
2646
2647 if (pending & SDMMC_INT_CMD_DONE) {
2648 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002649 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002650 }
2651
2652 if (pending & SDMMC_INT_CD) {
2653 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002654 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002655 }
2656
Jaehoon Chung58870242017-06-05 13:41:31 +09002657 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2658 mci_writel(host, RINTSTS,
2659 SDMMC_INT_SDIO(slot->sdio_id));
2660 __dw_mci_enable_sdio_irq(slot, 0);
2661 sdio_signal_irq(slot->mmc);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302662 }
2663
Markos Chandras1fb5f682013-03-12 10:53:11 +00002664 }
Will Newtonf95f3852011-01-02 01:11:59 -05002665
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002666 if (host->use_dma != TRANS_MODE_IDMAC)
2667 return IRQ_HANDLED;
2668
2669 /* Handle IDMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002670 if (host->dma_64bit_address == 1) {
2671 pending = mci_readl(host, IDSTS64);
2672 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2673 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2674 SDMMC_IDMAC_INT_RI);
2675 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002676 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2677 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002678 }
2679 } else {
2680 pending = mci_readl(host, IDSTS);
2681 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2682 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2683 SDMMC_IDMAC_INT_RI);
2684 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002685 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2686 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002687 }
Will Newtonf95f3852011-01-02 01:11:59 -05002688 }
Will Newtonf95f3852011-01-02 01:11:59 -05002689
2690 return IRQ_HANDLED;
2691}
2692
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002693static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002694{
2695 struct mmc_host *mmc;
2696 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002697 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002698 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002699 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002700
Thomas Abraham4a909202012-09-17 18:16:35 +00002701 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002702 if (!mmc)
2703 return -ENOMEM;
2704
2705 slot = mmc_priv(mmc);
2706 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002707 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002708 slot->mmc = mmc;
2709 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002710 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002711
2712 mmc->ops = &dw_mci_ops;
David Woods852ff5f2017-05-26 17:53:20 -04002713 if (device_property_read_u32_array(host->dev, "clock-freq-min-max",
2714 freq, 2)) {
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002715 mmc->f_min = DW_MCI_FREQ_MIN;
2716 mmc->f_max = DW_MCI_FREQ_MAX;
2717 } else {
Jaehoon Chungb0230302016-11-17 16:40:40 +09002718 dev_info(host->dev,
2719 "'clock-freq-min-max' property was deprecated.\n");
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002720 mmc->f_min = freq[0];
2721 mmc->f_max = freq[1];
2722 }
Will Newtonf95f3852011-01-02 01:11:59 -05002723
Yuvaraj CD51da2242014-08-22 19:17:50 +05302724 /*if there are external regulators, get them*/
2725 ret = mmc_regulator_get_supply(mmc);
2726 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002727 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302728
2729 if (!mmc->ocr_avail)
2730 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002731
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002732 if (host->pdata->caps)
2733 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002734
Jaehoon Chung6024e162016-07-15 10:54:50 +09002735 /*
2736 * Support MMC_CAP_ERASE by default.
2737 * It needs to use trim/discard/erase commands.
2738 */
2739 mmc->caps |= MMC_CAP_ERASE;
2740
Abhilash Kesavanab269122012-11-19 10:26:21 +05302741 if (host->pdata->pm_caps)
2742 mmc->pm_caps = host->pdata->pm_caps;
2743
Thomas Abraham800d78b2012-09-17 18:16:42 +00002744 if (host->dev->of_node) {
2745 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2746 if (ctrl_id < 0)
2747 ctrl_id = 0;
2748 } else {
2749 ctrl_id = to_platform_device(host->dev)->id;
2750 }
James Hogancb27a842012-10-16 09:43:08 +01002751 if (drv_data && drv_data->caps)
2752 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002753
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002754 if (host->pdata->caps2)
2755 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002756
Doug Anderson3cf890f2014-08-25 11:19:04 -07002757 ret = mmc_of_parse(mmc);
2758 if (ret)
2759 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002760
Ulf Hansson32dba732017-04-18 13:29:20 +02002761 /* Process SDIO IRQs through the sdio_irq_work. */
2762 if (mmc->caps & MMC_CAP_SDIO_IRQ)
2763 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2764
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002765 /* Useful defaults if platform data is unset. */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002766 if (host->use_dma == TRANS_MODE_IDMAC) {
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002767 mmc->max_segs = host->ring_size;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002768 mmc->max_blk_size = 65535;
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002769 mmc->max_seg_size = 0x1000;
2770 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2771 mmc->max_blk_count = mmc->max_req_size / 512;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002772 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2773 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002774 mmc->max_blk_size = 65535;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002775 mmc->max_blk_count = 65535;
2776 mmc->max_req_size =
2777 mmc->max_blk_size * mmc->max_blk_count;
2778 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002779 } else {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002780 /* TRANS_MODE_PIO */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002781 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002782 mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002783 mmc->max_blk_count = 512;
2784 mmc->max_req_size = mmc->max_blk_size *
2785 mmc->max_blk_count;
2786 mmc->max_seg_size = mmc->max_req_size;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002787 }
Will Newtonf95f3852011-01-02 01:11:59 -05002788
Shawn Linc0834a52016-05-27 14:36:40 +08002789 dw_mci_get_cd(mmc);
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002790
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002791 ret = mmc_add_host(mmc);
2792 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002793 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002794
2795#if defined(CONFIG_DEBUG_FS)
2796 dw_mci_init_debugfs(slot);
2797#endif
2798
Will Newtonf95f3852011-01-02 01:11:59 -05002799 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002800
Doug Anderson3cf890f2014-08-25 11:19:04 -07002801err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002802 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302803 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002804}
2805
2806static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2807{
Will Newtonf95f3852011-01-02 01:11:59 -05002808 /* Debugfs stuff is cleaned up by mmc core */
2809 mmc_remove_host(slot->mmc);
2810 slot->host->slot[id] = NULL;
2811 mmc_free_host(slot->mmc);
2812}
2813
2814static void dw_mci_init_dma(struct dw_mci *host)
2815{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002816 int addr_config;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002817 struct device *dev = host->dev;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002818
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002819 /*
2820 * Check tansfer mode from HCON[17:16]
2821 * Clear the ambiguous description of dw_mmc databook:
2822 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2823 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2824 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2825 * 2b'11: Non DW DMA Interface -> pio only
2826 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2827 * simpler request/acknowledge handshake mechanism and both of them
2828 * are regarded as external dma master for dw_mmc.
2829 */
2830 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2831 if (host->use_dma == DMA_INTERFACE_IDMA) {
2832 host->use_dma = TRANS_MODE_IDMAC;
2833 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2834 host->use_dma == DMA_INTERFACE_GDMA) {
2835 host->use_dma = TRANS_MODE_EDMAC;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002836 } else {
Will Newtonf95f3852011-01-02 01:11:59 -05002837 goto no_dma;
2838 }
2839
2840 /* Determine which DMA interface to use */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002841 if (host->use_dma == TRANS_MODE_IDMAC) {
2842 /*
2843 * Check ADDR_CONFIG bit in HCON to find
2844 * IDMAC address bus width
2845 */
Shawn Lin70692752015-09-16 14:41:37 +08002846 addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05002847
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002848 if (addr_config == 1) {
2849 /* host supports IDMAC in 64-bit address mode */
2850 host->dma_64bit_address = 1;
2851 dev_info(host->dev,
2852 "IDMAC supports 64-bit address mode.\n");
2853 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2854 dma_set_coherent_mask(host->dev,
2855 DMA_BIT_MASK(64));
2856 } else {
2857 /* host supports IDMAC in 32-bit address mode */
2858 host->dma_64bit_address = 0;
2859 dev_info(host->dev,
2860 "IDMAC supports 32-bit address mode.\n");
2861 }
2862
2863 /* Alloc memory for sg translation */
Shawn Lincc190d42016-09-02 12:14:39 +08002864 host->sg_cpu = dmam_alloc_coherent(host->dev,
2865 DESC_RING_BUF_SZ,
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002866 &host->sg_dma, GFP_KERNEL);
2867 if (!host->sg_cpu) {
2868 dev_err(host->dev,
2869 "%s: could not alloc DMA memory\n",
2870 __func__);
2871 goto no_dma;
2872 }
2873
2874 host->dma_ops = &dw_mci_idmac_ops;
2875 dev_info(host->dev, "Using internal DMA controller.\n");
2876 } else {
2877 /* TRANS_MODE_EDMAC: check dma bindings again */
David Woods852ff5f2017-05-26 17:53:20 -04002878 if ((device_property_read_string_array(dev, "dma-names",
2879 NULL, 0) < 0) ||
2880 !device_property_present(dev, "dmas")) {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002881 goto no_dma;
2882 }
2883 host->dma_ops = &dw_mci_edmac_ops;
2884 dev_info(host->dev, "Using external DMA controller.\n");
2885 }
Will Newtonf95f3852011-01-02 01:11:59 -05002886
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002887 if (host->dma_ops->init && host->dma_ops->start &&
2888 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002889 if (host->dma_ops->init(host)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002890 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2891 __func__);
Will Newtonf95f3852011-01-02 01:11:59 -05002892 goto no_dma;
2893 }
2894 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002895 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002896 goto no_dma;
2897 }
2898
Will Newtonf95f3852011-01-02 01:11:59 -05002899 return;
2900
2901no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002902 dev_info(host->dev, "Using PIO mode.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002903 host->use_dma = TRANS_MODE_PIO;
Will Newtonf95f3852011-01-02 01:11:59 -05002904}
2905
Doug Anderson5c935162015-03-09 16:18:21 -07002906static void dw_mci_cmd11_timer(unsigned long arg)
2907{
2908 struct dw_mci *host = (struct dw_mci *)arg;
2909
Doug Andersonfd674192015-04-03 11:13:06 -07002910 if (host->state != STATE_SENDING_CMD11) {
2911 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2912 return;
2913 }
Doug Anderson5c935162015-03-09 16:18:21 -07002914
2915 host->cmd_status = SDMMC_INT_RTO;
2916 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2917 tasklet_schedule(&host->tasklet);
2918}
2919
Addy Ke57e10482015-08-11 01:27:18 +09002920static void dw_mci_dto_timer(unsigned long arg)
2921{
2922 struct dw_mci *host = (struct dw_mci *)arg;
2923
2924 switch (host->state) {
2925 case STATE_SENDING_DATA:
2926 case STATE_DATA_BUSY:
2927 /*
2928 * If DTO interrupt does NOT come in sending data state,
2929 * we should notify the driver to terminate current transfer
2930 * and report a data timeout to the core.
2931 */
2932 host->data_status = SDMMC_INT_DRTO;
2933 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2934 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2935 tasklet_schedule(&host->tasklet);
2936 break;
2937 default:
2938 break;
2939 }
2940}
2941
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002942#ifdef CONFIG_OF
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002943static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2944{
2945 struct dw_mci_board *pdata;
2946 struct device *dev = host->dev;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002947 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Line8cc37b2016-01-21 14:52:52 +08002948 int ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002949 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002950
2951 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +09002952 if (!pdata)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002953 return ERR_PTR(-ENOMEM);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002954
Guodong Xud6786fe2016-08-12 16:51:26 +08002955 /* find reset controller when exist */
Jaehoon Chung3a667e32016-10-31 11:49:42 +09002956 pdata->rstc = devm_reset_control_get_optional(dev, "reset");
Guodong Xud6786fe2016-08-12 16:51:26 +08002957 if (IS_ERR(pdata->rstc)) {
2958 if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
2959 return ERR_PTR(-EPROBE_DEFER);
2960 }
2961
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002962 /* find out number of slots supported */
Jaehoon Chungd30a8f72017-06-05 13:41:30 +09002963 if (device_property_read_u32(dev, "num-slots", &pdata->num_slots))
2964 dev_info(dev, "'num-slots' was deprecated.\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002965
David Woods852ff5f2017-05-26 17:53:20 -04002966 if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002967 dev_info(dev,
2968 "fifo-depth property not found, using value of FIFOTH register as default\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002969
David Woods852ff5f2017-05-26 17:53:20 -04002970 device_property_read_u32(dev, "card-detect-delay",
2971 &pdata->detect_delay_ms);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002972
David Woods852ff5f2017-05-26 17:53:20 -04002973 device_property_read_u32(dev, "data-addr", &host->data_addr_override);
Jun Niea0361c12017-01-11 15:35:35 +09002974
David Woods852ff5f2017-05-26 17:53:20 -04002975 if (device_property_present(dev, "fifo-watermark-aligned"))
Jun Nied6fced82017-01-11 15:37:26 +09002976 host->wm_aligned = true;
2977
David Woods852ff5f2017-05-26 17:53:20 -04002978 if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002979 pdata->bus_hz = clock_frequency;
2980
James Hogancb27a842012-10-16 09:43:08 +01002981 if (drv_data && drv_data->parse_dt) {
2982 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002983 if (ret)
2984 return ERR_PTR(ret);
2985 }
2986
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002987 return pdata;
2988}
2989
2990#else /* CONFIG_OF */
2991static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2992{
2993 return ERR_PTR(-EINVAL);
2994}
2995#endif /* CONFIG_OF */
2996
Doug Andersonfa0c3282015-02-25 10:11:51 -08002997static void dw_mci_enable_cd(struct dw_mci *host)
2998{
Doug Andersonfa0c3282015-02-25 10:11:51 -08002999 unsigned long irqflags;
3000 u32 temp;
Jaehoon Chung58870242017-06-05 13:41:31 +09003001 int i = 0;
Shawn Line8cc37b2016-01-21 14:52:52 +08003002 struct dw_mci_slot *slot;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003003
Shawn Line8cc37b2016-01-21 14:52:52 +08003004 /*
3005 * No need for CD if all slots have a non-error GPIO
3006 * as well as broken card detection is found.
3007 */
Jaehoon Chung58870242017-06-05 13:41:31 +09003008 slot = host->slot[i];
3009 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
Doug Andersonfa0c3282015-02-25 10:11:51 -08003010 return;
3011
Jaehoon Chung58870242017-06-05 13:41:31 +09003012 if (mmc_gpio_get_cd(slot->mmc) < 0) {
3013 spin_lock_irqsave(&host->irq_lock, irqflags);
3014 temp = mci_readl(host, INTMASK);
3015 temp |= SDMMC_INT_CD;
3016 mci_writel(host, INTMASK, temp);
3017 spin_unlock_irqrestore(&host->irq_lock, irqflags);
3018 }
Doug Andersonfa0c3282015-02-25 10:11:51 -08003019}
3020
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303021int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003022{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00003023 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303024 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003025 u32 fifo_size;
3026
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003027 if (!host->pdata) {
3028 host->pdata = dw_mci_parse_dt(host);
Guodong Xud6786fe2016-08-12 16:51:26 +08003029 if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3030 return -EPROBE_DEFER;
3031 } else if (IS_ERR(host->pdata)) {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003032 dev_err(host->dev, "platform data not available\n");
3033 return -EINVAL;
3034 }
Will Newtonf95f3852011-01-02 01:11:59 -05003035 }
3036
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003037 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003038 if (IS_ERR(host->biu_clk)) {
3039 dev_dbg(host->dev, "biu clock not available\n");
3040 } else {
3041 ret = clk_prepare_enable(host->biu_clk);
3042 if (ret) {
3043 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003044 return ret;
3045 }
Will Newtonf95f3852011-01-02 01:11:59 -05003046 }
3047
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003048 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003049 if (IS_ERR(host->ciu_clk)) {
3050 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003051 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003052 } else {
3053 ret = clk_prepare_enable(host->ciu_clk);
3054 if (ret) {
3055 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003056 goto err_clk_biu;
3057 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003058
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003059 if (host->pdata->bus_hz) {
3060 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3061 if (ret)
3062 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003063 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003064 host->pdata->bus_hz);
3065 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003066 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003067 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003068
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003069 if (!host->bus_hz) {
3070 dev_err(host->dev,
3071 "Platform data must supply bus speed\n");
3072 ret = -ENODEV;
3073 goto err_clk_ciu;
3074 }
3075
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09003076 if (drv_data && drv_data->init) {
3077 ret = drv_data->init(host);
3078 if (ret) {
3079 dev_err(host->dev,
3080 "implementation specific init failed\n");
3081 goto err_clk_ciu;
3082 }
3083 }
3084
Guodong Xud6786fe2016-08-12 16:51:26 +08003085 if (!IS_ERR(host->pdata->rstc)) {
3086 reset_control_assert(host->pdata->rstc);
3087 usleep_range(10, 50);
3088 reset_control_deassert(host->pdata->rstc);
3089 }
3090
Doug Anderson5c935162015-03-09 16:18:21 -07003091 setup_timer(&host->cmd11_timer,
3092 dw_mci_cmd11_timer, (unsigned long)host);
3093
Jaehoon Chung16a34572016-06-21 14:35:37 +09003094 setup_timer(&host->dto_timer,
3095 dw_mci_dto_timer, (unsigned long)host);
Addy Ke57e10482015-08-11 01:27:18 +09003096
Will Newtonf95f3852011-01-02 01:11:59 -05003097 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08003098 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05003099 INIT_LIST_HEAD(&host->queue);
3100
Will Newtonf95f3852011-01-02 01:11:59 -05003101 /*
3102 * Get the host data width - this assumes that HCON has been set with
3103 * the correct values.
3104 */
Shawn Lin70692752015-09-16 14:41:37 +08003105 i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05003106 if (!i) {
3107 host->push_data = dw_mci_push_data16;
3108 host->pull_data = dw_mci_pull_data16;
3109 width = 16;
3110 host->data_shift = 1;
3111 } else if (i == 2) {
3112 host->push_data = dw_mci_push_data64;
3113 host->pull_data = dw_mci_pull_data64;
3114 width = 64;
3115 host->data_shift = 3;
3116 } else {
3117 /* Check for a reserved value, and warn if it is */
3118 WARN((i != 1),
3119 "HCON reports a reserved host data width!\n"
3120 "Defaulting to 32-bit access.\n");
3121 host->push_data = dw_mci_push_data32;
3122 host->pull_data = dw_mci_pull_data32;
3123 width = 32;
3124 host->data_shift = 2;
3125 }
3126
3127 /* Reset all blocks */
Shawn Lin37444152016-01-22 15:43:12 +08003128 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3129 ret = -ENODEV;
3130 goto err_clk_ciu;
3131 }
Seungwon Jeon141a7122012-05-22 13:01:03 +09003132
3133 host->dma_ops = host->pdata->dma_ops;
3134 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05003135
3136 /* Clear the interrupts for the host controller */
3137 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3138 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3139
3140 /* Put in max timeout */
3141 mci_writel(host, TMOUT, 0xFFFFFFFF);
3142
3143 /*
3144 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3145 * Tx Mark = fifo_size / 2 DMA Size = 8
3146 */
James Hoganb86d8252011-06-24 13:57:18 +01003147 if (!host->pdata->fifo_depth) {
3148 /*
3149 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3150 * have been overwritten by the bootloader, just like we're
3151 * about to do, so if you know the value for your hardware, you
3152 * should put it in the platform data.
3153 */
3154 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00003155 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01003156 } else {
3157 fifo_size = host->pdata->fifo_depth;
3158 }
3159 host->fifo_depth = fifo_size;
Seungwon Jeon524268992013-08-31 00:13:42 +09003160 host->fifoth_val =
3161 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003162 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05003163
3164 /* disable clock to CIU */
3165 mci_writel(host, CLKENA, 0);
3166 mci_writel(host, CLKSRC, 0);
3167
James Hogan63008762013-03-12 10:43:54 +00003168 /*
3169 * In 2.40a spec, Data offset is changed.
3170 * Need to check the version-id and set data-offset for DATA register.
3171 */
3172 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3173 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3174
Jun Niea0361c12017-01-11 15:35:35 +09003175 if (host->data_addr_override)
3176 host->fifo_reg = host->regs + host->data_addr_override;
3177 else if (host->verid < DW_MMC_240A)
Ben Dooks76184ac2015-03-25 11:27:52 +00003178 host->fifo_reg = host->regs + DATA_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003179 else
Ben Dooks76184ac2015-03-25 11:27:52 +00003180 host->fifo_reg = host->regs + DATA_240A_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003181
Will Newtonf95f3852011-01-02 01:11:59 -05003182 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003183 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3184 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05003185 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07003186 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003187
Jaehoon Chungd30a8f72017-06-05 13:41:30 +09003188 /*
Doug Andersonfa0c3282015-02-25 10:11:51 -08003189 * Enable interrupts for command done, data over, data empty,
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303190 * receive ready and error such as transmit, receive timeout, crc error
3191 */
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303192 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3193 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003194 DW_MCI_ERROR_FLAGS);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003195 /* Enable mci interrupt */
3196 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303197
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003198 dev_info(host->dev,
3199 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303200 host->irq, width, fifo_size);
3201
Will Newtonf95f3852011-01-02 01:11:59 -05003202 /* We need at least one slot to succeed */
Jaehoon Chung58870242017-06-05 13:41:31 +09003203 ret = dw_mci_init_slot(host, 0);
3204 if (ret) {
3205 dev_dbg(host->dev, "slot %d init failed\n", i);
Doug Anderson6130e7a2014-10-14 09:33:09 -07003206 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003207 }
3208
Doug Andersonb793f652015-03-11 15:15:14 -07003209 /* Now that slots are all setup, we can enable card detect */
3210 dw_mci_enable_cd(host);
3211
Will Newtonf95f3852011-01-02 01:11:59 -05003212 return 0;
3213
Will Newtonf95f3852011-01-02 01:11:59 -05003214err_dmaunmap:
3215 if (host->use_dma && host->dma_ops->exit)
3216 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003217
Guodong Xud6786fe2016-08-12 16:51:26 +08003218 if (!IS_ERR(host->pdata->rstc))
3219 reset_control_assert(host->pdata->rstc);
3220
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003221err_clk_ciu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003222 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003223
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003224err_clk_biu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003225 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003226
Will Newtonf95f3852011-01-02 01:11:59 -05003227 return ret;
3228}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303229EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05003230
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303231void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003232{
Jaehoon Chung58870242017-06-05 13:41:31 +09003233 int i = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003234
Jaehoon Chung58870242017-06-05 13:41:31 +09003235 dev_dbg(host->dev, "remove slot %d\n", i);
3236 if (host->slot[i])
3237 dw_mci_cleanup_slot(host->slot[i], i);
Will Newtonf95f3852011-01-02 01:11:59 -05003238
Prabu Thangamuthu048fd7e2015-05-28 12:21:06 +00003239 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3240 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3241
Will Newtonf95f3852011-01-02 01:11:59 -05003242 /* disable clock to CIU */
3243 mci_writel(host, CLKENA, 0);
3244 mci_writel(host, CLKSRC, 0);
3245
Will Newtonf95f3852011-01-02 01:11:59 -05003246 if (host->use_dma && host->dma_ops->exit)
3247 host->dma_ops->exit(host);
3248
Guodong Xud6786fe2016-08-12 16:51:26 +08003249 if (!IS_ERR(host->pdata->rstc))
3250 reset_control_assert(host->pdata->rstc);
3251
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003252 clk_disable_unprepare(host->ciu_clk);
3253 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05003254}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303255EXPORT_SYMBOL(dw_mci_remove);
3256
3257
Will Newtonf95f3852011-01-02 01:11:59 -05003258
Shawn Line9ed8832016-10-12 10:50:35 +08003259#ifdef CONFIG_PM
Shawn Lined24e1f2016-10-12 10:56:55 +08003260int dw_mci_runtime_suspend(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003261{
Shawn Lined24e1f2016-10-12 10:56:55 +08003262 struct dw_mci *host = dev_get_drvdata(dev);
3263
Shawn Lin3fc7eae2015-09-16 14:41:23 +08003264 if (host->use_dma && host->dma_ops->exit)
3265 host->dma_ops->exit(host);
3266
Shawn Lined24e1f2016-10-12 10:56:55 +08003267 clk_disable_unprepare(host->ciu_clk);
3268
3269 if (host->cur_slot &&
3270 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3271 !mmc_card_is_removable(host->cur_slot->mmc)))
3272 clk_disable_unprepare(host->biu_clk);
3273
Will Newtonf95f3852011-01-02 01:11:59 -05003274 return 0;
3275}
Shawn Lined24e1f2016-10-12 10:56:55 +08003276EXPORT_SYMBOL(dw_mci_runtime_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05003277
Shawn Lined24e1f2016-10-12 10:56:55 +08003278int dw_mci_runtime_resume(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003279{
Jaehoon Chung58870242017-06-05 13:41:31 +09003280 int i = 0, ret = 0;
Shawn Lined24e1f2016-10-12 10:56:55 +08003281 struct dw_mci *host = dev_get_drvdata(dev);
Jaehoon Chung58870242017-06-05 13:41:31 +09003282 struct dw_mci_slot *slot = host->slot[i];
Will Newtonf95f3852011-01-02 01:11:59 -05003283
Shawn Lined24e1f2016-10-12 10:56:55 +08003284 if (host->cur_slot &&
3285 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3286 !mmc_card_is_removable(host->cur_slot->mmc))) {
3287 ret = clk_prepare_enable(host->biu_clk);
3288 if (ret)
3289 return ret;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003290 }
3291
Shawn Lined24e1f2016-10-12 10:56:55 +08003292 ret = clk_prepare_enable(host->ciu_clk);
3293 if (ret)
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003294 goto err;
3295
3296 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3297 clk_disable_unprepare(host->ciu_clk);
3298 ret = -ENODEV;
3299 goto err;
3300 }
Shawn Lined24e1f2016-10-12 10:56:55 +08003301
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04003302 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09003303 host->dma_ops->init(host);
3304
Seungwon Jeon524268992013-08-31 00:13:42 +09003305 /*
3306 * Restore the initial value at FIFOTH register
3307 * And Invalidate the prev_blksz with zero
3308 */
Shawn Lined24e1f2016-10-12 10:56:55 +08003309 mci_writel(host, FIFOTH, host->fifoth_val);
3310 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003311
Doug Anderson2eb29442013-08-31 00:11:49 +09003312 /* Put in max timeout */
3313 mci_writel(host, TMOUT, 0xFFFFFFFF);
3314
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003315 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3316 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3317 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003318 DW_MCI_ERROR_FLAGS);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003319 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3320
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003321
Jaehoon Chung58870242017-06-05 13:41:31 +09003322 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
3323 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
Ziyuan Xue9748e02017-01-17 09:22:56 +08003324
Jaehoon Chung58870242017-06-05 13:41:31 +09003325 /* Force setup bus to guarantee available clock output */
3326 dw_mci_setup_bus(slot, true);
Doug Andersonfa0c3282015-02-25 10:11:51 -08003327
3328 /* Now that slots are all setup, we can enable card detect */
3329 dw_mci_enable_cd(host);
3330
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003331 return 0;
3332
3333err:
3334 if (host->cur_slot &&
3335 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3336 !mmc_card_is_removable(host->cur_slot->mmc)))
3337 clk_disable_unprepare(host->biu_clk);
3338
Shawn Lined24e1f2016-10-12 10:56:55 +08003339 return ret;
Shawn Line9ed8832016-10-12 10:50:35 +08003340}
3341EXPORT_SYMBOL(dw_mci_runtime_resume);
3342#endif /* CONFIG_PM */
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003343
Will Newtonf95f3852011-01-02 01:11:59 -05003344static int __init dw_mci_init(void)
3345{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05303346 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303347 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003348}
3349
3350static void __exit dw_mci_exit(void)
3351{
Will Newtonf95f3852011-01-02 01:11:59 -05003352}
3353
3354module_init(dw_mci_init);
3355module_exit(dw_mci_exit);
3356
3357MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3358MODULE_AUTHOR("NXP Semiconductor VietNam");
3359MODULE_AUTHOR("Imagination Technologies Ltd");
3360MODULE_LICENSE("GPL v2");