blob: e057f557ffab14a2f131e9be67f01ffe058e5b03 [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
Sonny Rao536f6b92014-10-16 09:58:05 -0700110static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800111static int dw_mci_card_busy(struct mmc_host *mmc);
Shawn Lin56f69112016-05-27 14:37:05 +0800112static int dw_mci_get_cd(struct mmc_host *mmc);
Seungwon Jeon31bff452013-08-31 00:14:23 +0900113
Will Newtonf95f3852011-01-02 01:11:59 -0500114#if defined(CONFIG_DEBUG_FS)
115static int dw_mci_req_show(struct seq_file *s, void *v)
116{
117 struct dw_mci_slot *slot = s->private;
118 struct mmc_request *mrq;
119 struct mmc_command *cmd;
120 struct mmc_command *stop;
121 struct mmc_data *data;
122
123 /* Make sure we get a consistent snapshot */
124 spin_lock_bh(&slot->host->lock);
125 mrq = slot->mrq;
126
127 if (mrq) {
128 cmd = mrq->cmd;
129 data = mrq->data;
130 stop = mrq->stop;
131
132 if (cmd)
133 seq_printf(s,
134 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
135 cmd->opcode, cmd->arg, cmd->flags,
136 cmd->resp[0], cmd->resp[1], cmd->resp[2],
137 cmd->resp[2], cmd->error);
138 if (data)
139 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
140 data->bytes_xfered, data->blocks,
141 data->blksz, data->flags, data->error);
142 if (stop)
143 seq_printf(s,
144 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
145 stop->opcode, stop->arg, stop->flags,
146 stop->resp[0], stop->resp[1], stop->resp[2],
147 stop->resp[2], stop->error);
148 }
149
150 spin_unlock_bh(&slot->host->lock);
151
152 return 0;
153}
154
155static int dw_mci_req_open(struct inode *inode, struct file *file)
156{
157 return single_open(file, dw_mci_req_show, inode->i_private);
158}
159
160static const struct file_operations dw_mci_req_fops = {
161 .owner = THIS_MODULE,
162 .open = dw_mci_req_open,
163 .read = seq_read,
164 .llseek = seq_lseek,
165 .release = single_release,
166};
167
168static int dw_mci_regs_show(struct seq_file *s, void *v)
169{
Jaehoon Chung21657ebd2016-11-17 16:40:33 +0900170 struct dw_mci *host = s->private;
171
172 seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
173 seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
174 seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
175 seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
176 seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
177 seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
Will Newtonf95f3852011-01-02 01:11:59 -0500178
179 return 0;
180}
181
182static int dw_mci_regs_open(struct inode *inode, struct file *file)
183{
184 return single_open(file, dw_mci_regs_show, inode->i_private);
185}
186
187static const struct file_operations dw_mci_regs_fops = {
188 .owner = THIS_MODULE,
189 .open = dw_mci_regs_open,
190 .read = seq_read,
191 .llseek = seq_lseek,
192 .release = single_release,
193};
194
195static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
196{
197 struct mmc_host *mmc = slot->mmc;
198 struct dw_mci *host = slot->host;
199 struct dentry *root;
200 struct dentry *node;
201
202 root = mmc->debugfs_root;
203 if (!root)
204 return;
205
206 node = debugfs_create_file("regs", S_IRUSR, root, host,
207 &dw_mci_regs_fops);
208 if (!node)
209 goto err;
210
211 node = debugfs_create_file("req", S_IRUSR, root, slot,
212 &dw_mci_req_fops);
213 if (!node)
214 goto err;
215
216 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
217 if (!node)
218 goto err;
219
220 node = debugfs_create_x32("pending_events", S_IRUSR, root,
221 (u32 *)&host->pending_events);
222 if (!node)
223 goto err;
224
225 node = debugfs_create_x32("completed_events", S_IRUSR, root,
226 (u32 *)&host->completed_events);
227 if (!node)
228 goto err;
229
230 return;
231
232err:
233 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
234}
235#endif /* defined(CONFIG_DEBUG_FS) */
236
Doug Anderson01730552014-08-22 19:17:51 +0530237static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
238
Will Newtonf95f3852011-01-02 01:11:59 -0500239static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
240{
Thomas Abraham800d78b2012-09-17 18:16:42 +0000241 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530242 struct dw_mci *host = slot->host;
Will Newtonf95f3852011-01-02 01:11:59 -0500243 u32 cmdr;
Will Newtonf95f3852011-01-02 01:11:59 -0500244
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800245 cmd->error = -EINPROGRESS;
Will Newtonf95f3852011-01-02 01:11:59 -0500246 cmdr = cmd->opcode;
247
Seungwon Jeon90c21432013-08-31 00:14:05 +0900248 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
249 cmd->opcode == MMC_GO_IDLE_STATE ||
250 cmd->opcode == MMC_GO_INACTIVE_STATE ||
251 (cmd->opcode == SD_IO_RW_DIRECT &&
252 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500253 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900254 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
255 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500256
Doug Anderson01730552014-08-22 19:17:51 +0530257 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
258 u32 clk_en_a;
259
260 /* Special bit makes CMD11 not die */
261 cmdr |= SDMMC_CMD_VOLT_SWITCH;
262
263 /* Change state to continue to handle CMD11 weirdness */
264 WARN_ON(slot->host->state != STATE_SENDING_CMD);
265 slot->host->state = STATE_SENDING_CMD11;
266
267 /*
268 * We need to disable low power mode (automatic clock stop)
269 * while doing voltage switch so we don't confuse the card,
270 * since stopping the clock is a specific part of the UHS
271 * voltage change dance.
272 *
273 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
274 * unconditionally turned back on in dw_mci_setup_bus() if it's
275 * ever called with a non-zero clock. That shouldn't happen
276 * until the voltage change is all done.
277 */
278 clk_en_a = mci_readl(host, CLKENA);
279 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
280 mci_writel(host, CLKENA, clk_en_a);
281 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
282 SDMMC_CMD_PRV_DAT_WAIT, 0);
283 }
284
Will Newtonf95f3852011-01-02 01:11:59 -0500285 if (cmd->flags & MMC_RSP_PRESENT) {
286 /* We expect a response, so set this bit */
287 cmdr |= SDMMC_CMD_RESP_EXP;
288 if (cmd->flags & MMC_RSP_136)
289 cmdr |= SDMMC_CMD_RESP_LONG;
290 }
291
292 if (cmd->flags & MMC_RSP_CRC)
293 cmdr |= SDMMC_CMD_RESP_CRC;
294
Jaehoon Chung0349c082016-11-17 16:40:39 +0900295 if (cmd->data) {
Will Newtonf95f3852011-01-02 01:11:59 -0500296 cmdr |= SDMMC_CMD_DAT_EXP;
Jaehoon Chung0349c082016-11-17 16:40:39 +0900297 if (cmd->data->flags & MMC_DATA_WRITE)
Will Newtonf95f3852011-01-02 01:11:59 -0500298 cmdr |= SDMMC_CMD_DAT_WR;
299 }
300
Jaehoon Chungaaaaeb72016-01-21 11:01:06 +0900301 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
302 cmdr |= SDMMC_CMD_USE_HOLD_REG;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000303
Will Newtonf95f3852011-01-02 01:11:59 -0500304 return cmdr;
305}
306
Seungwon Jeon90c21432013-08-31 00:14:05 +0900307static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
308{
309 struct mmc_command *stop;
310 u32 cmdr;
311
312 if (!cmd->data)
313 return 0;
314
315 stop = &host->stop_abort;
316 cmdr = cmd->opcode;
317 memset(stop, 0, sizeof(struct mmc_command));
318
319 if (cmdr == MMC_READ_SINGLE_BLOCK ||
320 cmdr == MMC_READ_MULTIPLE_BLOCK ||
321 cmdr == MMC_WRITE_BLOCK ||
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100322 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
323 cmdr == MMC_SEND_TUNING_BLOCK ||
324 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
Seungwon Jeon90c21432013-08-31 00:14:05 +0900325 stop->opcode = MMC_STOP_TRANSMISSION;
326 stop->arg = 0;
327 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
328 } else if (cmdr == SD_IO_RW_EXTENDED) {
329 stop->opcode = SD_IO_RW_DIRECT;
330 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
331 ((cmd->arg >> 28) & 0x7);
332 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
333 } else {
334 return 0;
335 }
336
337 cmdr = stop->opcode | SDMMC_CMD_STOP |
338 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
339
Jaehoon Chung8c005b42016-11-17 16:40:36 +0900340 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags))
341 cmdr |= SDMMC_CMD_USE_HOLD_REG;
342
Seungwon Jeon90c21432013-08-31 00:14:05 +0900343 return cmdr;
344}
345
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800346static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
347{
Shawn Linb6d2d812017-02-17 10:56:39 +0800348 u32 status;
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800349
350 /*
351 * Databook says that before issuing a new data transfer command
352 * we need to check to see if the card is busy. Data transfer commands
353 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
354 *
355 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
356 * expected.
357 */
358 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
359 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
Shawn Linb6d2d812017-02-17 10:56:39 +0800360 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
361 status,
362 !(status & SDMMC_STATUS_BUSY),
363 10, 500 * USEC_PER_MSEC))
364 dev_err(host->dev, "Busy; trying anyway\n");
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800365 }
366}
367
Will Newtonf95f3852011-01-02 01:11:59 -0500368static void dw_mci_start_command(struct dw_mci *host,
369 struct mmc_command *cmd, u32 cmd_flags)
370{
371 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000372 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500373 "start command: ARGR=0x%08x CMDR=0x%08x\n",
374 cmd->arg, cmd_flags);
375
376 mci_writel(host, CMDARG, cmd->arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800377 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800378 dw_mci_wait_while_busy(host, cmd_flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500379
380 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
381}
382
Seungwon Jeon90c21432013-08-31 00:14:05 +0900383static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500384{
Jaehoon Chunge13c3c02016-11-17 16:40:37 +0900385 struct mmc_command *stop = &host->stop_abort;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800386
Seungwon Jeon90c21432013-08-31 00:14:05 +0900387 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500388}
389
390/* DMA interface functions */
391static void dw_mci_stop_dma(struct dw_mci *host)
392{
James Hogan03e8cb52011-06-29 09:28:43 +0100393 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500394 host->dma_ops->stop(host);
395 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500396 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900397
398 /* Data transfer was stopped by the interrupt handler */
399 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500400}
401
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900402static int dw_mci_get_dma_dir(struct mmc_data *data)
403{
404 if (data->flags & MMC_DATA_WRITE)
405 return DMA_TO_DEVICE;
406 else
407 return DMA_FROM_DEVICE;
408}
409
Will Newtonf95f3852011-01-02 01:11:59 -0500410static void dw_mci_dma_cleanup(struct dw_mci *host)
411{
412 struct mmc_data *data = host->data;
413
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900414 if (data && data->host_cookie == COOKIE_MAPPED) {
415 dma_unmap_sg(host->dev,
416 data->sg,
417 data->sg_len,
418 dw_mci_get_dma_dir(data));
419 data->host_cookie = COOKIE_UNMAPPED;
420 }
Will Newtonf95f3852011-01-02 01:11:59 -0500421}
422
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900423static void dw_mci_idmac_reset(struct dw_mci *host)
424{
425 u32 bmod = mci_readl(host, BMOD);
426 /* Software reset of DMA */
427 bmod |= SDMMC_IDMAC_SWRESET;
428 mci_writel(host, BMOD, bmod);
429}
430
Will Newtonf95f3852011-01-02 01:11:59 -0500431static void dw_mci_idmac_stop_dma(struct dw_mci *host)
432{
433 u32 temp;
434
435 /* Disable and reset the IDMAC interface */
436 temp = mci_readl(host, CTRL);
437 temp &= ~SDMMC_CTRL_USE_IDMAC;
438 temp |= SDMMC_CTRL_DMA_RESET;
439 mci_writel(host, CTRL, temp);
440
441 /* Stop the IDMAC running */
442 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900443 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900444 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500445 mci_writel(host, BMOD, temp);
446}
447
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800448static void dw_mci_dmac_complete_dma(void *arg)
Will Newtonf95f3852011-01-02 01:11:59 -0500449{
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800450 struct dw_mci *host = arg;
Will Newtonf95f3852011-01-02 01:11:59 -0500451 struct mmc_data *data = host->data;
452
Thomas Abraham4a909202012-09-17 18:16:35 +0000453 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500454
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800455 if ((host->use_dma == TRANS_MODE_EDMAC) &&
456 data && (data->flags & MMC_DATA_READ))
457 /* Invalidate cache after read */
458 dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
459 data->sg,
460 data->sg_len,
461 DMA_FROM_DEVICE);
462
Will Newtonf95f3852011-01-02 01:11:59 -0500463 host->dma_ops->cleanup(host);
464
465 /*
466 * If the card was removed, data will be NULL. No point in trying to
467 * send the stop command or waiting for NBUSY in this case.
468 */
469 if (data) {
470 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
471 tasklet_schedule(&host->tasklet);
472 }
473}
474
Will Newtonf95f3852011-01-02 01:11:59 -0500475static int dw_mci_idmac_init(struct dw_mci *host)
476{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800477 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500478
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000479 if (host->dma_64bit_address == 1) {
480 struct idmac_desc_64addr *p;
481 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800482 host->ring_size =
483 DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500484
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000485 /* Forward link the descriptor list */
486 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
487 i++, p++) {
488 p->des6 = (host->sg_dma +
489 (sizeof(struct idmac_desc_64addr) *
490 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500491
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000492 p->des7 = (u64)(host->sg_dma +
493 (sizeof(struct idmac_desc_64addr) *
494 (i + 1))) >> 32;
495 /* Initialize reserved and buffer size fields to "0" */
496 p->des1 = 0;
497 p->des2 = 0;
498 p->des3 = 0;
499 }
500
501 /* Set the last descriptor as the end-of-ring descriptor */
502 p->des6 = host->sg_dma & 0xffffffff;
503 p->des7 = (u64)host->sg_dma >> 32;
504 p->des0 = IDMAC_DES0_ER;
505
506 } else {
507 struct idmac_desc *p;
508 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800509 host->ring_size =
510 DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000511
512 /* Forward link the descriptor list */
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800513 for (i = 0, p = host->sg_cpu;
514 i < host->ring_size - 1;
515 i++, p++) {
Ben Dooks6687c422015-03-25 11:27:51 +0000516 p->des3 = cpu_to_le32(host->sg_dma +
517 (sizeof(struct idmac_desc) * (i + 1)));
Zhangfei Gao4b244722015-04-30 22:16:28 +0800518 p->des1 = 0;
519 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000520
521 /* Set the last descriptor as the end-of-ring descriptor */
Ben Dooks6687c422015-03-25 11:27:51 +0000522 p->des3 = cpu_to_le32(host->sg_dma);
523 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000524 }
Will Newtonf95f3852011-01-02 01:11:59 -0500525
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900526 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900527
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000528 if (host->dma_64bit_address == 1) {
529 /* Mask out interrupts - get Tx & Rx complete only */
530 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
531 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
532 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500533
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000534 /* Set the descriptor base address */
535 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
536 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
537
538 } else {
539 /* Mask out interrupts - get Tx & Rx complete only */
540 mci_writel(host, IDSTS, IDMAC_INT_CLR);
541 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
542 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
543
544 /* Set the descriptor base address */
545 mci_writel(host, DBADDR, host->sg_dma);
546 }
547
Will Newtonf95f3852011-01-02 01:11:59 -0500548 return 0;
549}
550
Shawn Lin3b2a0672016-09-02 12:14:37 +0800551static inline int dw_mci_prepare_desc64(struct dw_mci *host,
552 struct mmc_data *data,
553 unsigned int sg_len)
554{
555 unsigned int desc_len;
556 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
Shawn Linb6d2d812017-02-17 10:56:39 +0800557 u32 val;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800558 int i;
559
560 desc_first = desc_last = desc = host->sg_cpu;
561
562 for (i = 0; i < sg_len; i++) {
563 unsigned int length = sg_dma_len(&data->sg[i]);
564
565 u64 mem_addr = sg_dma_address(&data->sg[i]);
566
567 for ( ; length ; desc++) {
568 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
569 length : DW_MCI_DESC_DATA_LENGTH;
570
571 length -= desc_len;
572
573 /*
574 * Wait for the former clear OWN bit operation
575 * of IDMAC to make sure that this descriptor
576 * isn't still owned by IDMAC as IDMAC's write
577 * ops and CPU's read ops are asynchronous.
578 */
Shawn Linb6d2d812017-02-17 10:56:39 +0800579 if (readl_poll_timeout_atomic(&desc->des0, val,
580 !(val & IDMAC_DES0_OWN),
581 10, 100 * USEC_PER_MSEC))
582 goto err_own_bit;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800583
584 /*
585 * Set the OWN bit and disable interrupts
586 * for this descriptor
587 */
588 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
589 IDMAC_DES0_CH;
590
591 /* Buffer length */
592 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
593
594 /* Physical address to DMA to/from */
595 desc->des4 = mem_addr & 0xffffffff;
596 desc->des5 = mem_addr >> 32;
597
598 /* Update physical address for the next desc */
599 mem_addr += desc_len;
600
601 /* Save pointer to the last descriptor */
602 desc_last = desc;
603 }
604 }
605
606 /* Set first descriptor */
607 desc_first->des0 |= IDMAC_DES0_FD;
608
609 /* Set last descriptor */
610 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
611 desc_last->des0 |= IDMAC_DES0_LD;
612
613 return 0;
614err_own_bit:
615 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000616 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800617 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800618 dw_mci_idmac_init(host);
619 return -EINVAL;
620}
621
622
623static inline int dw_mci_prepare_desc32(struct dw_mci *host,
624 struct mmc_data *data,
625 unsigned int sg_len)
626{
627 unsigned int desc_len;
628 struct idmac_desc *desc_first, *desc_last, *desc;
Shawn Linb6d2d812017-02-17 10:56:39 +0800629 u32 val;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800630 int i;
631
632 desc_first = desc_last = desc = host->sg_cpu;
633
634 for (i = 0; i < sg_len; i++) {
635 unsigned int length = sg_dma_len(&data->sg[i]);
636
637 u32 mem_addr = sg_dma_address(&data->sg[i]);
638
639 for ( ; length ; desc++) {
640 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
641 length : DW_MCI_DESC_DATA_LENGTH;
642
643 length -= desc_len;
644
645 /*
646 * Wait for the former clear OWN bit operation
647 * of IDMAC to make sure that this descriptor
648 * isn't still owned by IDMAC as IDMAC's write
649 * ops and CPU's read ops are asynchronous.
650 */
Shawn Linb6d2d812017-02-17 10:56:39 +0800651 if (readl_poll_timeout_atomic(&desc->des0, val,
652 IDMAC_OWN_CLR64(val),
653 10,
654 100 * USEC_PER_MSEC))
655 goto err_own_bit;
Shawn Lin3b2a0672016-09-02 12:14:37 +0800656
657 /*
658 * Set the OWN bit and disable interrupts
659 * for this descriptor
660 */
661 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
662 IDMAC_DES0_DIC |
663 IDMAC_DES0_CH);
664
665 /* Buffer length */
666 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
667
668 /* Physical address to DMA to/from */
669 desc->des2 = cpu_to_le32(mem_addr);
670
671 /* Update physical address for the next desc */
672 mem_addr += desc_len;
673
674 /* Save pointer to the last descriptor */
675 desc_last = desc;
676 }
677 }
678
679 /* Set first descriptor */
680 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
681
682 /* Set last descriptor */
683 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
684 IDMAC_DES0_DIC));
685 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
686
687 return 0;
688err_own_bit:
689 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000690 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800691 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800692 dw_mci_idmac_init(host);
693 return -EINVAL;
694}
695
696static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
697{
698 u32 temp;
699 int ret;
700
701 if (host->dma_64bit_address == 1)
702 ret = dw_mci_prepare_desc64(host, host->data, sg_len);
703 else
704 ret = dw_mci_prepare_desc32(host, host->data, sg_len);
705
706 if (ret)
707 goto out;
708
709 /* drain writebuffer */
710 wmb();
711
712 /* Make sure to reset DMA in case we did PIO before this */
713 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
714 dw_mci_idmac_reset(host);
715
716 /* Select IDMAC interface */
717 temp = mci_readl(host, CTRL);
718 temp |= SDMMC_CTRL_USE_IDMAC;
719 mci_writel(host, CTRL, temp);
720
721 /* drain writebuffer */
722 wmb();
723
724 /* Enable the IDMAC */
725 temp = mci_readl(host, BMOD);
726 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
727 mci_writel(host, BMOD, temp);
728
729 /* Start it running */
730 mci_writel(host, PLDMND, 1);
731
732out:
733 return ret;
734}
735
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100736static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900737 .init = dw_mci_idmac_init,
738 .start = dw_mci_idmac_start_dma,
739 .stop = dw_mci_idmac_stop_dma,
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800740 .complete = dw_mci_dmac_complete_dma,
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900741 .cleanup = dw_mci_dma_cleanup,
742};
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800743
744static void dw_mci_edmac_stop_dma(struct dw_mci *host)
745{
Shawn Linab925a32016-03-09 10:34:46 +0800746 dmaengine_terminate_async(host->dms->ch);
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800747}
748
749static int dw_mci_edmac_start_dma(struct dw_mci *host,
750 unsigned int sg_len)
751{
752 struct dma_slave_config cfg;
753 struct dma_async_tx_descriptor *desc = NULL;
754 struct scatterlist *sgl = host->data->sg;
755 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
756 u32 sg_elems = host->data->sg_len;
757 u32 fifoth_val;
758 u32 fifo_offset = host->fifo_reg - host->regs;
759 int ret = 0;
760
761 /* Set external dma config: burst size, burst width */
Arnd Bergmann260b3162015-11-12 15:14:23 +0100762 cfg.dst_addr = host->phy_regs + fifo_offset;
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800763 cfg.src_addr = cfg.dst_addr;
764 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
765 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
766
767 /* Match burst msize with external dma config */
768 fifoth_val = mci_readl(host, FIFOTH);
769 cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
770 cfg.src_maxburst = cfg.dst_maxburst;
771
772 if (host->data->flags & MMC_DATA_WRITE)
773 cfg.direction = DMA_MEM_TO_DEV;
774 else
775 cfg.direction = DMA_DEV_TO_MEM;
776
777 ret = dmaengine_slave_config(host->dms->ch, &cfg);
778 if (ret) {
779 dev_err(host->dev, "Failed to config edmac.\n");
780 return -EBUSY;
781 }
782
783 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
784 sg_len, cfg.direction,
785 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
786 if (!desc) {
787 dev_err(host->dev, "Can't prepare slave sg.\n");
788 return -EBUSY;
789 }
790
791 /* Set dw_mci_dmac_complete_dma as callback */
792 desc->callback = dw_mci_dmac_complete_dma;
793 desc->callback_param = (void *)host;
794 dmaengine_submit(desc);
795
796 /* Flush cache before write */
797 if (host->data->flags & MMC_DATA_WRITE)
798 dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
799 sg_elems, DMA_TO_DEVICE);
800
801 dma_async_issue_pending(host->dms->ch);
802
803 return 0;
804}
805
806static int dw_mci_edmac_init(struct dw_mci *host)
807{
808 /* Request external dma channel */
809 host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
810 if (!host->dms)
811 return -ENOMEM;
812
813 host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
814 if (!host->dms->ch) {
Dan Carpenter4539d362015-10-22 22:53:46 +0300815 dev_err(host->dev, "Failed to get external DMA channel.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800816 kfree(host->dms);
817 host->dms = NULL;
818 return -ENXIO;
819 }
820
821 return 0;
822}
823
824static void dw_mci_edmac_exit(struct dw_mci *host)
825{
826 if (host->dms) {
827 if (host->dms->ch) {
828 dma_release_channel(host->dms->ch);
829 host->dms->ch = NULL;
830 }
831 kfree(host->dms);
832 host->dms = NULL;
833 }
834}
835
836static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
837 .init = dw_mci_edmac_init,
838 .exit = dw_mci_edmac_exit,
839 .start = dw_mci_edmac_start_dma,
840 .stop = dw_mci_edmac_stop_dma,
841 .complete = dw_mci_dmac_complete_dma,
842 .cleanup = dw_mci_dma_cleanup,
843};
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900844
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900845static int dw_mci_pre_dma_transfer(struct dw_mci *host,
846 struct mmc_data *data,
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900847 int cookie)
Will Newtonf95f3852011-01-02 01:11:59 -0500848{
849 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900850 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500851
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900852 if (data->host_cookie == COOKIE_PRE_MAPPED)
853 return data->sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500854
855 /*
856 * We don't do DMA on "complex" transfers, i.e. with
857 * non-word-aligned buffers or lengths. Also, we don't bother
858 * with all the DMA setup overhead for short transfers.
859 */
860 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
861 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900862
Will Newtonf95f3852011-01-02 01:11:59 -0500863 if (data->blksz & 3)
864 return -EINVAL;
865
866 for_each_sg(data->sg, sg, data->sg_len, i) {
867 if (sg->offset & 3 || sg->length & 3)
868 return -EINVAL;
869 }
870
Thomas Abraham4a909202012-09-17 18:16:35 +0000871 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900872 data->sg,
873 data->sg_len,
874 dw_mci_get_dma_dir(data));
875 if (sg_len == 0)
876 return -EINVAL;
877
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900878 data->host_cookie = cookie;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900879
880 return sg_len;
881}
882
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900883static void dw_mci_pre_req(struct mmc_host *mmc,
Linus Walleijd3c6aac2016-11-23 11:02:24 +0100884 struct mmc_request *mrq)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900885{
886 struct dw_mci_slot *slot = mmc_priv(mmc);
887 struct mmc_data *data = mrq->data;
888
889 if (!slot->host->use_dma || !data)
890 return;
891
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900892 /* This data might be unmapped at this time */
893 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900894
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900895 if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
896 COOKIE_PRE_MAPPED) < 0)
897 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900898}
899
900static void dw_mci_post_req(struct mmc_host *mmc,
901 struct mmc_request *mrq,
902 int err)
903{
904 struct dw_mci_slot *slot = mmc_priv(mmc);
905 struct mmc_data *data = mrq->data;
906
907 if (!slot->host->use_dma || !data)
908 return;
909
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900910 if (data->host_cookie != COOKIE_UNMAPPED)
Thomas Abraham4a909202012-09-17 18:16:35 +0000911 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900912 data->sg,
913 data->sg_len,
914 dw_mci_get_dma_dir(data));
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900915 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900916}
917
Seungwon Jeon52426892013-08-31 00:13:42 +0900918static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
919{
Seungwon Jeon52426892013-08-31 00:13:42 +0900920 unsigned int blksz = data->blksz;
921 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
922 u32 fifo_width = 1 << host->data_shift;
923 u32 blksz_depth = blksz / fifo_width, fifoth_val;
924 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800925 int idx = ARRAY_SIZE(mszs) - 1;
Seungwon Jeon52426892013-08-31 00:13:42 +0900926
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800927 /* pio should ship this scenario */
928 if (!host->use_dma)
929 return;
930
Seungwon Jeon52426892013-08-31 00:13:42 +0900931 tx_wmark = (host->fifo_depth) / 2;
932 tx_wmark_invers = host->fifo_depth - tx_wmark;
933
934 /*
935 * MSIZE is '1',
936 * if blksz is not a multiple of the FIFO width
937 */
Shawn Lin20753562016-09-21 10:40:25 +0800938 if (blksz % fifo_width)
Seungwon Jeon52426892013-08-31 00:13:42 +0900939 goto done;
Seungwon Jeon52426892013-08-31 00:13:42 +0900940
941 do {
942 if (!((blksz_depth % mszs[idx]) ||
943 (tx_wmark_invers % mszs[idx]))) {
944 msize = idx;
945 rx_wmark = mszs[idx] - 1;
946 break;
947 }
948 } while (--idx > 0);
949 /*
950 * If idx is '0', it won't be tried
951 * Thus, initial values are uesed
952 */
953done:
954 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
955 mci_writel(host, FIFOTH, fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +0900956}
957
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900958static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900959{
960 unsigned int blksz = data->blksz;
961 u32 blksz_depth, fifo_depth;
962 u16 thld_size;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900963 u8 enable;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900964
James Hogan66dfd102014-11-17 17:49:05 +0000965 /*
966 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
967 * in the FIFO region, so we really shouldn't access it).
968 */
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900969 if (host->verid < DW_MMC_240A ||
970 (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
James Hogan66dfd102014-11-17 17:49:05 +0000971 return;
972
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900973 /*
974 * Card write Threshold is introduced since 2.80a
975 * It's used when HS400 mode is enabled.
976 */
977 if (data->flags & MMC_DATA_WRITE &&
978 !(host->timing != MMC_TIMING_MMC_HS400))
979 return;
980
981 if (data->flags & MMC_DATA_WRITE)
982 enable = SDMMC_CARD_WR_THR_EN;
983 else
984 enable = SDMMC_CARD_RD_THR_EN;
985
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900986 if (host->timing != MMC_TIMING_MMC_HS200 &&
987 host->timing != MMC_TIMING_UHS_SDR104)
988 goto disable;
989
990 blksz_depth = blksz / (1 << host->data_shift);
991 fifo_depth = host->fifo_depth;
992
993 if (blksz_depth > fifo_depth)
994 goto disable;
995
996 /*
997 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
998 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
999 * Currently just choose blksz.
1000 */
1001 thld_size = blksz;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001002 mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001003 return;
1004
1005disable:
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001006 mci_writel(host, CDTHRCTL, 0);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001007}
1008
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001009static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
1010{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001011 unsigned long irqflags;
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001012 int sg_len;
1013 u32 temp;
1014
1015 host->using_dma = 0;
1016
1017 /* If we don't have a channel, we can't do DMA */
1018 if (!host->use_dma)
1019 return -ENODEV;
1020
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +09001021 sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001022 if (sg_len < 0) {
1023 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001024 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001025 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001026
James Hogan03e8cb52011-06-29 09:28:43 +01001027 host->using_dma = 1;
1028
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001029 if (host->use_dma == TRANS_MODE_IDMAC)
1030 dev_vdbg(host->dev,
1031 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1032 (unsigned long)host->sg_cpu,
1033 (unsigned long)host->sg_dma,
1034 sg_len);
Will Newtonf95f3852011-01-02 01:11:59 -05001035
Seungwon Jeon52426892013-08-31 00:13:42 +09001036 /*
1037 * Decide the MSIZE and RX/TX Watermark.
1038 * If current block size is same with previous size,
1039 * no need to update fifoth.
1040 */
1041 if (host->prev_blksz != data->blksz)
1042 dw_mci_adjust_fifoth(host, data);
1043
Will Newtonf95f3852011-01-02 01:11:59 -05001044 /* Enable the DMA interface */
1045 temp = mci_readl(host, CTRL);
1046 temp |= SDMMC_CTRL_DMA_ENABLE;
1047 mci_writel(host, CTRL, temp);
1048
1049 /* Disable RX/TX IRQs, let DMA handle it */
Doug Andersonf8c58c12014-12-02 15:42:47 -08001050 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001051 temp = mci_readl(host, INTMASK);
1052 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1053 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001054 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001055
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001056 if (host->dma_ops->start(host, sg_len)) {
Jaehoon Chung647f80a2016-11-21 10:51:48 +09001057 host->dma_ops->stop(host);
Shawn Lind12d0cb2016-09-02 12:14:38 +08001058 /* We can't do DMA, try PIO for this one */
1059 dev_dbg(host->dev,
1060 "%s: fall back to PIO mode for current transfer\n",
1061 __func__);
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001062 return -ENODEV;
1063 }
Will Newtonf95f3852011-01-02 01:11:59 -05001064
1065 return 0;
1066}
1067
1068static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1069{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001070 unsigned long irqflags;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001071 int flags = SG_MITER_ATOMIC;
Will Newtonf95f3852011-01-02 01:11:59 -05001072 u32 temp;
1073
1074 data->error = -EINPROGRESS;
1075
1076 WARN_ON(host->data);
1077 host->sg = NULL;
1078 host->data = data;
1079
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001080 if (data->flags & MMC_DATA_READ)
James Hogan55c5efbc2011-06-29 09:29:58 +01001081 host->dir_status = DW_MCI_RECV_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001082 else
James Hogan55c5efbc2011-06-29 09:29:58 +01001083 host->dir_status = DW_MCI_SEND_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001084
1085 dw_mci_ctrl_thld(host, data);
James Hogan55c5efbc2011-06-29 09:29:58 +01001086
Will Newtonf95f3852011-01-02 01:11:59 -05001087 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001088 if (host->data->flags & MMC_DATA_READ)
1089 flags |= SG_MITER_TO_SG;
1090 else
1091 flags |= SG_MITER_FROM_SG;
1092
1093 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001094 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +01001095 host->part_buf_start = 0;
1096 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001097
James Hoganb40af3a2011-06-24 13:54:06 +01001098 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001099
1100 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001101 temp = mci_readl(host, INTMASK);
1102 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1103 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001104 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001105
1106 temp = mci_readl(host, CTRL);
1107 temp &= ~SDMMC_CTRL_DMA_ENABLE;
1108 mci_writel(host, CTRL, temp);
Seungwon Jeon52426892013-08-31 00:13:42 +09001109
1110 /*
Jun Nied6fced82017-01-11 15:37:26 +09001111 * Use the initial fifoth_val for PIO mode. If wm_algined
1112 * is set, we set watermark same as data size.
Seungwon Jeon52426892013-08-31 00:13:42 +09001113 * If next issued data may be transfered by DMA mode,
1114 * prev_blksz should be invalidated.
1115 */
Jun Nied6fced82017-01-11 15:37:26 +09001116 if (host->wm_aligned)
1117 dw_mci_adjust_fifoth(host, data);
1118 else
1119 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +09001120 host->prev_blksz = 0;
1121 } else {
1122 /*
1123 * Keep the current block size.
1124 * It will be used to decide whether to update
1125 * fifoth register next time.
1126 */
1127 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -05001128 }
1129}
1130
1131static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
1132{
1133 struct dw_mci *host = slot->host;
Will Newtonf95f3852011-01-02 01:11:59 -05001134 unsigned int cmd_status = 0;
1135
1136 mci_writel(host, CMDARG, arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001137 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -08001138 dw_mci_wait_while_busy(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001139 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
1140
Shawn Linb6d2d812017-02-17 10:56:39 +08001141 if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
1142 !(cmd_status & SDMMC_CMD_START),
1143 1, 500 * USEC_PER_MSEC))
1144 dev_err(&slot->mmc->class_dev,
1145 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
1146 cmd, arg, cmd_status);
Will Newtonf95f3852011-01-02 01:11:59 -05001147}
1148
Abhilash Kesavanab269122012-11-19 10:26:21 +05301149static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -05001150{
1151 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +09001152 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001153 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001154 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +05301155 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1156
1157 /* We must continue to set bit 28 in CMD until the change is complete */
1158 if (host->state == STATE_WAITING_CMD11_DONE)
1159 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -05001160
Doug Andersonfdf492a2013-08-31 00:11:43 +09001161 if (!clock) {
1162 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +05301163 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +09001164 } else if (clock != host->current_speed || force_clkinit) {
1165 div = host->bus_hz / clock;
1166 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -05001167 /*
1168 * move the + 1 after the divide to prevent
1169 * over-clocking the card.
1170 */
Seungwon Jeone4199902012-05-22 13:01:21 +09001171 div += 1;
1172
Doug Andersonfdf492a2013-08-31 00:11:43 +09001173 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001174
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001175 if ((clock != slot->__clk_old &&
1176 !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1177 force_clkinit) {
Shawn Lince69e2f2017-01-17 09:22:55 +08001178 /* Silent the verbose log if calling from PM context */
1179 if (!force_clkinit)
1180 dev_info(&slot->mmc->class_dev,
1181 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1182 slot->id, host->bus_hz, clock,
1183 div ? ((host->bus_hz / div) >> 1) :
1184 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -05001185
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001186 /*
1187 * If card is polling, display the message only
1188 * one time at boot time.
1189 */
1190 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1191 slot->mmc->f_min == clock)
1192 set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1193 }
1194
Will Newtonf95f3852011-01-02 01:11:59 -05001195 /* disable clock */
1196 mci_writel(host, CLKENA, 0);
1197 mci_writel(host, CLKSRC, 0);
1198
1199 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301200 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001201
1202 /* set clock to desired speed */
1203 mci_writel(host, CLKDIV, div);
1204
1205 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301206 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001207
Doug Anderson9623b5b2012-07-25 08:33:17 -07001208 /* enable clock; only low power if no SDIO */
1209 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -08001210 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -07001211 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1212 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -05001213
1214 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301215 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Jaehoon Chung005d6752016-09-22 14:12:00 +09001216
1217 /* keep the last clock value that was requested from core */
1218 slot->__clk_old = clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001219 }
1220
Doug Andersonfdf492a2013-08-31 00:11:43 +09001221 host->current_speed = clock;
1222
Will Newtonf95f3852011-01-02 01:11:59 -05001223 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +09001224 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -05001225}
1226
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001227static void __dw_mci_start_request(struct dw_mci *host,
1228 struct dw_mci_slot *slot,
1229 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001230{
1231 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001232 struct mmc_data *data;
1233 u32 cmdflags;
1234
1235 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001236
Will Newtonf95f3852011-01-02 01:11:59 -05001237 host->cur_slot = slot;
1238 host->mrq = mrq;
1239
1240 host->pending_events = 0;
1241 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001242 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001243 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001244 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001245
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001246 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -05001247 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +09001248 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -05001249 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1250 mci_writel(host, BLKSIZ, data->blksz);
1251 }
1252
Will Newtonf95f3852011-01-02 01:11:59 -05001253 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1254
1255 /* this is the first command, send the initialization clock */
1256 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1257 cmdflags |= SDMMC_CMD_INIT;
1258
1259 if (data) {
1260 dw_mci_submit_data(host, data);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001261 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05001262 }
1263
1264 dw_mci_start_command(host, cmd, cmdflags);
1265
Doug Anderson5c935162015-03-09 16:18:21 -07001266 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
Doug Anderson49ba0302015-04-03 11:13:07 -07001267 unsigned long irqflags;
1268
Doug Anderson5c935162015-03-09 16:18:21 -07001269 /*
Doug Anderson8886a6f2015-04-03 11:13:05 -07001270 * Databook says to fail after 2ms w/ no response, but evidence
1271 * shows that sometimes the cmd11 interrupt takes over 130ms.
1272 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1273 * is just about to roll over.
Doug Anderson49ba0302015-04-03 11:13:07 -07001274 *
1275 * We do this whole thing under spinlock and only if the
1276 * command hasn't already completed (indicating the the irq
1277 * already ran so we don't want the timeout).
Doug Anderson5c935162015-03-09 16:18:21 -07001278 */
Doug Anderson49ba0302015-04-03 11:13:07 -07001279 spin_lock_irqsave(&host->irq_lock, irqflags);
1280 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1281 mod_timer(&host->cmd11_timer,
1282 jiffies + msecs_to_jiffies(500) + 1);
1283 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Doug Anderson5c935162015-03-09 16:18:21 -07001284 }
1285
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001286 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001287}
1288
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001289static void dw_mci_start_request(struct dw_mci *host,
1290 struct dw_mci_slot *slot)
1291{
1292 struct mmc_request *mrq = slot->mrq;
1293 struct mmc_command *cmd;
1294
1295 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1296 __dw_mci_start_request(host, slot, cmd);
1297}
1298
James Hogan7456caa2011-06-24 13:55:10 +01001299/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001300static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1301 struct mmc_request *mrq)
1302{
1303 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1304 host->state);
1305
Will Newtonf95f3852011-01-02 01:11:59 -05001306 slot->mrq = mrq;
1307
Doug Anderson01730552014-08-22 19:17:51 +05301308 if (host->state == STATE_WAITING_CMD11_DONE) {
1309 dev_warn(&slot->mmc->class_dev,
1310 "Voltage change didn't complete\n");
1311 /*
1312 * this case isn't expected to happen, so we can
1313 * either crash here or just try to continue on
1314 * in the closest possible state
1315 */
1316 host->state = STATE_IDLE;
1317 }
1318
Will Newtonf95f3852011-01-02 01:11:59 -05001319 if (host->state == STATE_IDLE) {
1320 host->state = STATE_SENDING_CMD;
1321 dw_mci_start_request(host, slot);
1322 } else {
1323 list_add_tail(&slot->queue_node, &host->queue);
1324 }
Will Newtonf95f3852011-01-02 01:11:59 -05001325}
1326
1327static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1328{
1329 struct dw_mci_slot *slot = mmc_priv(mmc);
1330 struct dw_mci *host = slot->host;
1331
1332 WARN_ON(slot->mrq);
1333
James Hogan7456caa2011-06-24 13:55:10 +01001334 /*
1335 * The check for card presence and queueing of the request must be
1336 * atomic, otherwise the card could be removed in between and the
1337 * request wouldn't fail until another card was inserted.
1338 */
James Hogan7456caa2011-06-24 13:55:10 +01001339
Shawn Lin56f69112016-05-27 14:37:05 +08001340 if (!dw_mci_get_cd(mmc)) {
Will Newtonf95f3852011-01-02 01:11:59 -05001341 mrq->cmd->error = -ENOMEDIUM;
1342 mmc_request_done(mmc, mrq);
1343 return;
1344 }
1345
Shawn Lin56f69112016-05-27 14:37:05 +08001346 spin_lock_bh(&host->lock);
1347
Will Newtonf95f3852011-01-02 01:11:59 -05001348 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001349
1350 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001351}
1352
1353static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1354{
1355 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001356 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001357 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301358 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001359
Will Newtonf95f3852011-01-02 01:11:59 -05001360 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001361 case MMC_BUS_WIDTH_4:
1362 slot->ctype = SDMMC_CTYPE_4BIT;
1363 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001364 case MMC_BUS_WIDTH_8:
1365 slot->ctype = SDMMC_CTYPE_8BIT;
1366 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001367 default:
1368 /* set default 1 bit mode */
1369 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001370 }
1371
Seungwon Jeon3f514292012-01-02 16:00:02 +09001372 regs = mci_readl(slot->host, UHS_REG);
1373
Jaehoon Chung41babf72011-02-24 13:46:11 +09001374 /* DDR mode set */
Seungwon Jeon80113132015-01-29 08:11:57 +05301375 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
Jaehoon Chung7cc8d582015-10-21 19:49:42 +09001376 ios->timing == MMC_TIMING_UHS_DDR50 ||
Seungwon Jeon80113132015-01-29 08:11:57 +05301377 ios->timing == MMC_TIMING_MMC_HS400)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001378 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001379 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001380 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001381
1382 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001383 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001384
Doug Andersonfdf492a2013-08-31 00:11:43 +09001385 /*
1386 * Use mirror of ios->clock to prevent race with mmc
1387 * core ios update when finding the minimum.
1388 */
1389 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001390
James Hogancb27a842012-10-16 09:43:08 +01001391 if (drv_data && drv_data->set_ios)
1392 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001393
Will Newtonf95f3852011-01-02 01:11:59 -05001394 switch (ios->power_mode) {
1395 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301396 if (!IS_ERR(mmc->supply.vmmc)) {
1397 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1398 ios->vdd);
1399 if (ret) {
1400 dev_err(slot->host->dev,
1401 "failed to enable vmmc regulator\n");
1402 /*return, if failed turn on vmmc*/
1403 return;
1404 }
1405 }
Doug Anderson29d0d162015-01-13 15:58:44 -08001406 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1407 regs = mci_readl(slot->host, PWREN);
1408 regs |= (1 << slot->id);
1409 mci_writel(slot->host, PWREN, regs);
1410 break;
1411 case MMC_POWER_ON:
Doug Andersond1f1dd82015-02-20 10:57:19 -08001412 if (!slot->host->vqmmc_enabled) {
1413 if (!IS_ERR(mmc->supply.vqmmc)) {
1414 ret = regulator_enable(mmc->supply.vqmmc);
1415 if (ret < 0)
1416 dev_err(slot->host->dev,
1417 "failed to enable vqmmc\n");
1418 else
1419 slot->host->vqmmc_enabled = true;
1420
1421 } else {
1422 /* Keep track so we don't reset again */
Yuvaraj CD51da2242014-08-22 19:17:50 +05301423 slot->host->vqmmc_enabled = true;
Doug Andersond1f1dd82015-02-20 10:57:19 -08001424 }
1425
1426 /* Reset our state machine after powering on */
1427 dw_mci_ctrl_reset(slot->host,
1428 SDMMC_CTRL_ALL_RESET_FLAGS);
Yuvaraj CD51da2242014-08-22 19:17:50 +05301429 }
Doug Anderson655babb2015-02-20 10:57:18 -08001430
1431 /* Adjust clock / bus width after power is up */
1432 dw_mci_setup_bus(slot, false);
1433
James Hogane6f34e22013-03-12 10:43:32 +00001434 break;
1435 case MMC_POWER_OFF:
Doug Anderson655babb2015-02-20 10:57:18 -08001436 /* Turn clock off before power goes down */
1437 dw_mci_setup_bus(slot, false);
1438
Yuvaraj CD51da2242014-08-22 19:17:50 +05301439 if (!IS_ERR(mmc->supply.vmmc))
1440 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1441
Doug Andersond1f1dd82015-02-20 10:57:19 -08001442 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
Yuvaraj CD51da2242014-08-22 19:17:50 +05301443 regulator_disable(mmc->supply.vqmmc);
Doug Andersond1f1dd82015-02-20 10:57:19 -08001444 slot->host->vqmmc_enabled = false;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301445
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001446 regs = mci_readl(slot->host, PWREN);
1447 regs &= ~(1 << slot->id);
1448 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001449 break;
1450 default:
1451 break;
1452 }
Doug Anderson655babb2015-02-20 10:57:18 -08001453
1454 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1455 slot->host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001456}
1457
Doug Anderson01730552014-08-22 19:17:51 +05301458static int dw_mci_card_busy(struct mmc_host *mmc)
1459{
1460 struct dw_mci_slot *slot = mmc_priv(mmc);
1461 u32 status;
1462
1463 /*
1464 * Check the busy bit which is low when DAT[3:0]
1465 * (the data lines) are 0000
1466 */
1467 status = mci_readl(slot->host, STATUS);
1468
1469 return !!(status & SDMMC_STATUS_BUSY);
1470}
1471
1472static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1473{
1474 struct dw_mci_slot *slot = mmc_priv(mmc);
1475 struct dw_mci *host = slot->host;
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001476 const struct dw_mci_drv_data *drv_data = host->drv_data;
Doug Anderson01730552014-08-22 19:17:51 +05301477 u32 uhs;
1478 u32 v18 = SDMMC_UHS_18V << slot->id;
Doug Anderson01730552014-08-22 19:17:51 +05301479 int ret;
1480
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001481 if (drv_data && drv_data->switch_voltage)
1482 return drv_data->switch_voltage(mmc, ios);
1483
Doug Anderson01730552014-08-22 19:17:51 +05301484 /*
1485 * Program the voltage. Note that some instances of dw_mmc may use
1486 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1487 * does no harm but you need to set the regulator directly. Try both.
1488 */
1489 uhs = mci_readl(host, UHS_REG);
Douglas Andersone0848f52015-10-12 14:48:26 +02001490 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
Doug Anderson01730552014-08-22 19:17:51 +05301491 uhs &= ~v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001492 else
Doug Anderson01730552014-08-22 19:17:51 +05301493 uhs |= v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001494
Doug Anderson01730552014-08-22 19:17:51 +05301495 if (!IS_ERR(mmc->supply.vqmmc)) {
Douglas Andersone0848f52015-10-12 14:48:26 +02001496 ret = mmc_regulator_set_vqmmc(mmc, ios);
Doug Anderson01730552014-08-22 19:17:51 +05301497
1498 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001499 dev_dbg(&mmc->class_dev,
Douglas Andersone0848f52015-10-12 14:48:26 +02001500 "Regulator set error %d - %s V\n",
1501 ret, uhs & v18 ? "1.8" : "3.3");
Doug Anderson01730552014-08-22 19:17:51 +05301502 return ret;
1503 }
1504 }
1505 mci_writel(host, UHS_REG, uhs);
1506
1507 return 0;
1508}
1509
Will Newtonf95f3852011-01-02 01:11:59 -05001510static int dw_mci_get_ro(struct mmc_host *mmc)
1511{
1512 int read_only;
1513 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001514 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001515
1516 /* Use platform get_ro function, else try on board write protect */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001517 if (gpio_ro >= 0)
Jaehoon Chung9795a842014-03-03 11:36:46 +09001518 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001519 else
1520 read_only =
1521 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1522
1523 dev_dbg(&mmc->class_dev, "card is %s\n",
1524 read_only ? "read-only" : "read-write");
1525
1526 return read_only;
1527}
1528
1529static int dw_mci_get_cd(struct mmc_host *mmc)
1530{
1531 int present;
1532 struct dw_mci_slot *slot = mmc_priv(mmc);
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001533 struct dw_mci *host = slot->host;
1534 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001535
1536 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001537 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
1538 || !mmc_card_is_removable(mmc))) {
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001539 present = 1;
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001540
1541 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1542 if (mmc->caps & MMC_CAP_NEEDS_POLL) {
1543 dev_info(&mmc->class_dev,
1544 "card is polling.\n");
1545 } else {
1546 dev_info(&mmc->class_dev,
1547 "card is non-removable.\n");
1548 }
1549 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1550 }
1551
1552 return present;
1553 } else if (gpio_cd >= 0)
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001554 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001555 else
1556 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1557 == 0 ? 1 : 0;
1558
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001559 spin_lock_bh(&host->lock);
Jaehoon Chung1f4d5072016-11-17 16:40:34 +09001560 if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001561 dev_dbg(&mmc->class_dev, "card is present\n");
Jaehoon Chung1c238a92016-11-24 20:04:40 +09001562 else if (!present &&
1563 !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001564 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001565 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001566
1567 return present;
1568}
1569
Shawn Lin935a6652016-01-14 09:08:02 +08001570static void dw_mci_hw_reset(struct mmc_host *mmc)
1571{
1572 struct dw_mci_slot *slot = mmc_priv(mmc);
1573 struct dw_mci *host = slot->host;
1574 int reset;
1575
1576 if (host->use_dma == TRANS_MODE_IDMAC)
1577 dw_mci_idmac_reset(host);
1578
1579 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1580 SDMMC_CTRL_FIFO_RESET))
1581 return;
1582
1583 /*
1584 * According to eMMC spec, card reset procedure:
1585 * tRstW >= 1us: RST_n pulse width
1586 * tRSCA >= 200us: RST_n to Command time
1587 * tRSTH >= 1us: RST_n high period
1588 */
1589 reset = mci_readl(host, RST_N);
1590 reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1591 mci_writel(host, RST_N, reset);
1592 usleep_range(1, 2);
1593 reset |= SDMMC_RST_HWACTIVE << slot->id;
1594 mci_writel(host, RST_N, reset);
1595 usleep_range(200, 300);
1596}
1597
Doug Andersonb24c8b22014-12-02 15:42:46 -08001598static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001599{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001600 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001601 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001602
Doug Andersonb24c8b22014-12-02 15:42:46 -08001603 /*
1604 * Low power mode will stop the card clock when idle. According to the
1605 * description of the CLKENA register we should disable low power mode
1606 * for SDIO cards if we need SDIO interrupts to work.
1607 */
1608 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1609 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1610 u32 clk_en_a_old;
1611 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001612
Doug Andersonb24c8b22014-12-02 15:42:46 -08001613 clk_en_a_old = mci_readl(host, CLKENA);
1614
1615 if (card->type == MMC_TYPE_SDIO ||
1616 card->type == MMC_TYPE_SD_COMBO) {
Douglas Andersona6db2c82017-04-11 15:55:43 -07001617 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
1618 pm_runtime_get_noresume(mmc->parent);
1619 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1620 }
Doug Andersonb24c8b22014-12-02 15:42:46 -08001621 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1622 } else {
Douglas Andersona6db2c82017-04-11 15:55:43 -07001623 if (test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
1624 pm_runtime_put_noidle(mmc->parent);
1625 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1626 }
Doug Andersonb24c8b22014-12-02 15:42:46 -08001627 clk_en_a = clk_en_a_old | clken_low_pwr;
1628 }
1629
1630 if (clk_en_a != clk_en_a_old) {
1631 mci_writel(host, CLKENA, clk_en_a);
1632 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1633 SDMMC_CMD_PRV_DAT_WAIT, 0);
1634 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001635 }
1636}
1637
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301638static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1639{
1640 struct dw_mci_slot *slot = mmc_priv(mmc);
1641 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
Seungwon Jeon0976f162013-08-31 00:12:42 +09001658static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1659{
1660 struct dw_mci_slot *slot = mmc_priv(mmc);
1661 struct dw_mci *host = slot->host;
1662 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001663 int err = -EINVAL;
Seungwon Jeon0976f162013-08-31 00:12:42 +09001664
Seungwon Jeon0976f162013-08-31 00:12:42 +09001665 if (drv_data && drv_data->execute_tuning)
Chaotian Jing9979dbe2015-10-27 14:24:28 +08001666 err = drv_data->execute_tuning(slot, opcode);
Seungwon Jeon0976f162013-08-31 00:12:42 +09001667 return err;
1668}
1669
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001670static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1671 struct mmc_ios *ios)
Seungwon Jeon80113132015-01-29 08:11:57 +05301672{
1673 struct dw_mci_slot *slot = mmc_priv(mmc);
1674 struct dw_mci *host = slot->host;
1675 const struct dw_mci_drv_data *drv_data = host->drv_data;
1676
1677 if (drv_data && drv_data->prepare_hs400_tuning)
1678 return drv_data->prepare_hs400_tuning(host, ios);
1679
1680 return 0;
1681}
1682
Shawn Lin4e7392b2017-02-17 10:56:40 +08001683static bool dw_mci_reset(struct dw_mci *host)
1684{
1685 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
1686 bool ret = false;
1687
1688 /*
1689 * Resetting generates a block interrupt, hence setting
1690 * the scatter-gather pointer to NULL.
1691 */
1692 if (host->sg) {
1693 sg_miter_stop(&host->sg_miter);
1694 host->sg = NULL;
1695 }
1696
1697 if (host->use_dma)
1698 flags |= SDMMC_CTRL_DMA_RESET;
1699
1700 if (dw_mci_ctrl_reset(host, flags)) {
1701 /*
1702 * In all cases we clear the RAWINTS register to clear any
1703 * interrupts.
1704 */
1705 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1706
1707 /* if using dma we wait for dma_req to clear */
1708 if (host->use_dma) {
1709 u32 status;
1710
1711 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
1712 status,
1713 !(status & SDMMC_STATUS_DMA_REQ),
1714 1, 500 * USEC_PER_MSEC)) {
1715 dev_err(host->dev,
1716 "%s: Timeout waiting for dma_req to clear during reset\n",
1717 __func__);
1718 goto ciu_out;
1719 }
1720
1721 /* when using DMA next we reset the fifo again */
1722 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
1723 goto ciu_out;
1724 }
1725 } else {
1726 /* if the controller reset bit did clear, then set clock regs */
1727 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
1728 dev_err(host->dev,
1729 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
1730 __func__);
1731 goto ciu_out;
1732 }
1733 }
1734
1735 if (host->use_dma == TRANS_MODE_IDMAC)
1736 /* It is also recommended that we reset and reprogram idmac */
1737 dw_mci_idmac_reset(host);
1738
1739 ret = true;
1740
1741ciu_out:
1742 /* After a CTRL reset we need to have CIU set clock registers */
1743 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
1744
1745 return ret;
1746}
1747
Will Newtonf95f3852011-01-02 01:11:59 -05001748static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301749 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001750 .pre_req = dw_mci_pre_req,
1751 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301752 .set_ios = dw_mci_set_ios,
1753 .get_ro = dw_mci_get_ro,
1754 .get_cd = dw_mci_get_cd,
Shawn Lin935a6652016-01-14 09:08:02 +08001755 .hw_reset = dw_mci_hw_reset,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301756 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001757 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301758 .card_busy = dw_mci_card_busy,
1759 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001760 .init_card = dw_mci_init_card,
Seungwon Jeon80113132015-01-29 08:11:57 +05301761 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
Will Newtonf95f3852011-01-02 01:11:59 -05001762};
1763
1764static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1765 __releases(&host->lock)
1766 __acquires(&host->lock)
1767{
1768 struct dw_mci_slot *slot;
1769 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1770
1771 WARN_ON(host->cmd || host->data);
1772
1773 host->cur_slot->mrq = NULL;
1774 host->mrq = NULL;
1775 if (!list_empty(&host->queue)) {
1776 slot = list_entry(host->queue.next,
1777 struct dw_mci_slot, queue_node);
1778 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001779 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001780 mmc_hostname(slot->mmc));
1781 host->state = STATE_SENDING_CMD;
1782 dw_mci_start_request(host, slot);
1783 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001784 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301785
1786 if (host->state == STATE_SENDING_CMD11)
1787 host->state = STATE_WAITING_CMD11_DONE;
1788 else
1789 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001790 }
1791
1792 spin_unlock(&host->lock);
1793 mmc_request_done(prev_mmc, mrq);
1794 spin_lock(&host->lock);
1795}
1796
Seungwon Jeone352c812013-08-31 00:14:17 +09001797static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001798{
1799 u32 status = host->cmd_status;
1800
1801 host->cmd_status = 0;
1802
1803 /* Read the response from the card (up to 16 bytes) */
1804 if (cmd->flags & MMC_RSP_PRESENT) {
1805 if (cmd->flags & MMC_RSP_136) {
1806 cmd->resp[3] = mci_readl(host, RESP0);
1807 cmd->resp[2] = mci_readl(host, RESP1);
1808 cmd->resp[1] = mci_readl(host, RESP2);
1809 cmd->resp[0] = mci_readl(host, RESP3);
1810 } else {
1811 cmd->resp[0] = mci_readl(host, RESP0);
1812 cmd->resp[1] = 0;
1813 cmd->resp[2] = 0;
1814 cmd->resp[3] = 0;
1815 }
1816 }
1817
1818 if (status & SDMMC_INT_RTO)
1819 cmd->error = -ETIMEDOUT;
1820 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1821 cmd->error = -EILSEQ;
1822 else if (status & SDMMC_INT_RESP_ERR)
1823 cmd->error = -EIO;
1824 else
1825 cmd->error = 0;
1826
Seungwon Jeone352c812013-08-31 00:14:17 +09001827 return cmd->error;
1828}
1829
1830static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1831{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001832 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001833
1834 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1835 if (status & SDMMC_INT_DRTO) {
1836 data->error = -ETIMEDOUT;
1837 } else if (status & SDMMC_INT_DCRC) {
1838 data->error = -EILSEQ;
1839 } else if (status & SDMMC_INT_EBE) {
1840 if (host->dir_status ==
1841 DW_MCI_SEND_STATUS) {
1842 /*
1843 * No data CRC status was returned.
1844 * The number of bytes transferred
1845 * will be exaggerated in PIO mode.
1846 */
1847 data->bytes_xfered = 0;
1848 data->error = -ETIMEDOUT;
1849 } else if (host->dir_status ==
1850 DW_MCI_RECV_STATUS) {
Shawn Line7a1dec2016-08-22 10:57:16 +08001851 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001852 }
1853 } else {
1854 /* SDMMC_INT_SBE is included */
Shawn Line7a1dec2016-08-22 10:57:16 +08001855 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001856 }
1857
Doug Andersone6cc0122014-04-22 16:51:21 -07001858 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001859
1860 /*
1861 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001862 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001863 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001864 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001865 } else {
1866 data->bytes_xfered = data->blocks * data->blksz;
1867 data->error = 0;
1868 }
1869
1870 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001871}
1872
Addy Ke57e10482015-08-11 01:27:18 +09001873static void dw_mci_set_drto(struct dw_mci *host)
1874{
1875 unsigned int drto_clks;
1876 unsigned int drto_ms;
1877
1878 drto_clks = mci_readl(host, TMOUT) >> 8;
1879 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1880
1881 /* add a bit spare time */
1882 drto_ms += 10;
1883
1884 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1885}
1886
Will Newtonf95f3852011-01-02 01:11:59 -05001887static void dw_mci_tasklet_func(unsigned long priv)
1888{
1889 struct dw_mci *host = (struct dw_mci *)priv;
1890 struct mmc_data *data;
1891 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001892 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001893 enum dw_mci_state state;
1894 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001895 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001896
1897 spin_lock(&host->lock);
1898
1899 state = host->state;
1900 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001901 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001902
1903 do {
1904 prev_state = state;
1905
1906 switch (state) {
1907 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301908 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001909 break;
1910
Doug Anderson01730552014-08-22 19:17:51 +05301911 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001912 case STATE_SENDING_CMD:
1913 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1914 &host->pending_events))
1915 break;
1916
1917 cmd = host->cmd;
1918 host->cmd = NULL;
1919 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001920 err = dw_mci_command_complete(host, cmd);
1921 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001922 prev_state = state = STATE_SENDING_CMD;
1923 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001924 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001925 goto unlock;
1926 }
1927
Seungwon Jeone352c812013-08-31 00:14:17 +09001928 if (cmd->data && err) {
Doug Anderson46d17952016-04-26 10:03:58 +02001929 /*
1930 * During UHS tuning sequence, sending the stop
1931 * command after the response CRC error would
1932 * throw the system into a confused state
1933 * causing all future tuning phases to report
1934 * failure.
1935 *
1936 * In such case controller will move into a data
1937 * transfer state after a response error or
1938 * response CRC error. Let's let that finish
1939 * before trying to send a stop, so we'll go to
1940 * STATE_SENDING_DATA.
1941 *
1942 * Although letting the data transfer take place
1943 * will waste a bit of time (we already know
1944 * the command was bad), it can't cause any
1945 * errors since it's possible it would have
1946 * taken place anyway if this tasklet got
1947 * delayed. Allowing the transfer to take place
1948 * avoids races and keeps things simple.
1949 */
1950 if ((err != -ETIMEDOUT) &&
1951 (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
1952 state = STATE_SENDING_DATA;
1953 continue;
1954 }
1955
Seungwon Jeon71abb132013-08-31 00:13:59 +09001956 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001957 send_stop_abort(host, data);
1958 state = STATE_SENDING_STOP;
1959 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001960 }
1961
Seungwon Jeone352c812013-08-31 00:14:17 +09001962 if (!cmd->data || err) {
1963 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001964 goto unlock;
1965 }
1966
1967 prev_state = state = STATE_SENDING_DATA;
1968 /* fall through */
1969
1970 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001971 /*
1972 * We could get a data error and never a transfer
1973 * complete so we'd better check for it here.
1974 *
1975 * Note that we don't really care if we also got a
1976 * transfer complete; stopping the DMA and sending an
1977 * abort won't hurt.
1978 */
Will Newtonf95f3852011-01-02 01:11:59 -05001979 if (test_and_clear_bit(EVENT_DATA_ERROR,
1980 &host->pending_events)) {
1981 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001982 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08001983 SDMMC_INT_EBE)))
1984 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001985 state = STATE_DATA_ERROR;
1986 break;
1987 }
1988
1989 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001990 &host->pending_events)) {
1991 /*
1992 * If all data-related interrupts don't come
1993 * within the given time in reading data state.
1994 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09001995 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09001996 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001997 break;
Addy Ke57e10482015-08-11 01:27:18 +09001998 }
Will Newtonf95f3852011-01-02 01:11:59 -05001999
2000 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07002001
2002 /*
2003 * Handle an EVENT_DATA_ERROR that might have shown up
2004 * before the transfer completed. This might not have
2005 * been caught by the check above because the interrupt
2006 * could have gone off between the previous check and
2007 * the check for transfer complete.
2008 *
2009 * Technically this ought not be needed assuming we
2010 * get a DATA_COMPLETE eventually (we'll notice the
2011 * error and end the request), but it shouldn't hurt.
2012 *
2013 * This has the advantage of sending the stop command.
2014 */
2015 if (test_and_clear_bit(EVENT_DATA_ERROR,
2016 &host->pending_events)) {
2017 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002018 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08002019 SDMMC_INT_EBE)))
2020 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07002021 state = STATE_DATA_ERROR;
2022 break;
2023 }
Will Newtonf95f3852011-01-02 01:11:59 -05002024 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07002025
Will Newtonf95f3852011-01-02 01:11:59 -05002026 /* fall through */
2027
2028 case STATE_DATA_BUSY:
2029 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09002030 &host->pending_events)) {
2031 /*
2032 * If data error interrupt comes but data over
2033 * interrupt doesn't come within the given time.
2034 * in reading data state.
2035 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09002036 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09002037 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002038 break;
Addy Ke57e10482015-08-11 01:27:18 +09002039 }
Will Newtonf95f3852011-01-02 01:11:59 -05002040
2041 host->data = NULL;
2042 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09002043 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05002044
Seungwon Jeone352c812013-08-31 00:14:17 +09002045 if (!err) {
2046 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05302047 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002048 data->stop->error = 0;
2049 dw_mci_request_end(host, mrq);
2050 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05002051 }
Will Newtonf95f3852011-01-02 01:11:59 -05002052
Seungwon Jeon90c21432013-08-31 00:14:05 +09002053 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09002054 if (data->stop)
2055 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07002056 } else {
2057 /*
2058 * If we don't have a command complete now we'll
2059 * never get one since we just reset everything;
2060 * better end the request.
2061 *
2062 * If we do have a command complete we'll fall
2063 * through to the SENDING_STOP command and
2064 * everything will be peachy keen.
2065 */
2066 if (!test_bit(EVENT_CMD_COMPLETE,
2067 &host->pending_events)) {
2068 host->cmd = NULL;
2069 dw_mci_request_end(host, mrq);
2070 goto unlock;
2071 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09002072 }
Seungwon Jeone352c812013-08-31 00:14:17 +09002073
2074 /*
2075 * If err has non-zero,
2076 * stop-abort command has been already issued.
2077 */
2078 prev_state = state = STATE_SENDING_STOP;
2079
Will Newtonf95f3852011-01-02 01:11:59 -05002080 /* fall through */
2081
2082 case STATE_SENDING_STOP:
2083 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2084 &host->pending_events))
2085 break;
2086
Seungwon Jeon71abb132013-08-31 00:13:59 +09002087 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09002088 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07002089 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09002090
Will Newtonf95f3852011-01-02 01:11:59 -05002091 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09002092 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09002093
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002094 if (!mrq->sbc && mrq->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002095 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09002096 else
2097 host->cmd_status = 0;
2098
Seungwon Jeone352c812013-08-31 00:14:17 +09002099 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05002100 goto unlock;
2101
2102 case STATE_DATA_ERROR:
2103 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2104 &host->pending_events))
2105 break;
2106
2107 state = STATE_DATA_BUSY;
2108 break;
2109 }
2110 } while (state != prev_state);
2111
2112 host->state = state;
2113unlock:
2114 spin_unlock(&host->lock);
2115
2116}
2117
James Hogan34b664a2011-06-24 13:57:56 +01002118/* push final bytes to part_buf, only use during push */
2119static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2120{
2121 memcpy((void *)&host->part_buf, buf, cnt);
2122 host->part_buf_count = cnt;
2123}
2124
2125/* append bytes to part_buf, only use during push */
2126static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2127{
2128 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2129 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2130 host->part_buf_count += cnt;
2131 return cnt;
2132}
2133
2134/* pull first bytes from part_buf, only use during pull */
2135static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2136{
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002137 cnt = min_t(int, cnt, host->part_buf_count);
James Hogan34b664a2011-06-24 13:57:56 +01002138 if (cnt) {
2139 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2140 cnt);
2141 host->part_buf_count -= cnt;
2142 host->part_buf_start += cnt;
2143 }
2144 return cnt;
2145}
2146
2147/* pull final bytes from the part_buf, assuming it's just been filled */
2148static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2149{
2150 memcpy(buf, &host->part_buf, cnt);
2151 host->part_buf_start = cnt;
2152 host->part_buf_count = (1 << host->data_shift) - cnt;
2153}
2154
Will Newtonf95f3852011-01-02 01:11:59 -05002155static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2156{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002157 struct mmc_data *data = host->data;
2158 int init_cnt = cnt;
2159
James Hogan34b664a2011-06-24 13:57:56 +01002160 /* try and push anything in the part_buf */
2161 if (unlikely(host->part_buf_count)) {
2162 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002163
James Hogan34b664a2011-06-24 13:57:56 +01002164 buf += len;
2165 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002166 if (host->part_buf_count == 2) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002167 mci_fifo_writew(host->fifo_reg, host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01002168 host->part_buf_count = 0;
2169 }
2170 }
2171#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2172 if (unlikely((unsigned long)buf & 0x1)) {
2173 while (cnt >= 2) {
2174 u16 aligned_buf[64];
2175 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2176 int items = len >> 1;
2177 int i;
2178 /* memcpy from input buffer into aligned buffer */
2179 memcpy(aligned_buf, buf, len);
2180 buf += len;
2181 cnt -= len;
2182 /* push data from aligned buffer into fifo */
2183 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002184 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002185 }
2186 } else
2187#endif
2188 {
2189 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002190
James Hogan34b664a2011-06-24 13:57:56 +01002191 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002192 mci_fifo_writew(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002193 buf = pdata;
2194 }
2195 /* put anything remaining in the part_buf */
2196 if (cnt) {
2197 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002198 /* Push data if we have reached the expected data length */
2199 if ((data->bytes_xfered + init_cnt) ==
2200 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002201 mci_fifo_writew(host->fifo_reg, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05002202 }
2203}
2204
2205static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2206{
James Hogan34b664a2011-06-24 13:57:56 +01002207#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2208 if (unlikely((unsigned long)buf & 0x1)) {
2209 while (cnt >= 2) {
2210 /* pull data from fifo into aligned buffer */
2211 u16 aligned_buf[64];
2212 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2213 int items = len >> 1;
2214 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002215
James Hogan34b664a2011-06-24 13:57:56 +01002216 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002217 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002218 /* memcpy from aligned buffer into output buffer */
2219 memcpy(buf, aligned_buf, len);
2220 buf += len;
2221 cnt -= len;
2222 }
2223 } else
2224#endif
2225 {
2226 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002227
James Hogan34b664a2011-06-24 13:57:56 +01002228 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002229 *pdata++ = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002230 buf = pdata;
2231 }
2232 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002233 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002234 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002235 }
2236}
2237
2238static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2239{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002240 struct mmc_data *data = host->data;
2241 int init_cnt = cnt;
2242
James Hogan34b664a2011-06-24 13:57:56 +01002243 /* try and push anything in the part_buf */
2244 if (unlikely(host->part_buf_count)) {
2245 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002246
James Hogan34b664a2011-06-24 13:57:56 +01002247 buf += len;
2248 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002249 if (host->part_buf_count == 4) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002250 mci_fifo_writel(host->fifo_reg, host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01002251 host->part_buf_count = 0;
2252 }
2253 }
2254#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2255 if (unlikely((unsigned long)buf & 0x3)) {
2256 while (cnt >= 4) {
2257 u32 aligned_buf[32];
2258 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2259 int items = len >> 2;
2260 int i;
2261 /* memcpy from input buffer into aligned buffer */
2262 memcpy(aligned_buf, buf, len);
2263 buf += len;
2264 cnt -= len;
2265 /* push data from aligned buffer into fifo */
2266 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002267 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002268 }
2269 } else
2270#endif
2271 {
2272 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002273
James Hogan34b664a2011-06-24 13:57:56 +01002274 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002275 mci_fifo_writel(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002276 buf = pdata;
2277 }
2278 /* put anything remaining in the part_buf */
2279 if (cnt) {
2280 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002281 /* Push data if we have reached the expected data length */
2282 if ((data->bytes_xfered + init_cnt) ==
2283 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002284 mci_fifo_writel(host->fifo_reg, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05002285 }
2286}
2287
2288static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2289{
James Hogan34b664a2011-06-24 13:57:56 +01002290#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2291 if (unlikely((unsigned long)buf & 0x3)) {
2292 while (cnt >= 4) {
2293 /* pull data from fifo into aligned buffer */
2294 u32 aligned_buf[32];
2295 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2296 int items = len >> 2;
2297 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002298
James Hogan34b664a2011-06-24 13:57:56 +01002299 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002300 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002301 /* memcpy from aligned buffer into output buffer */
2302 memcpy(buf, aligned_buf, len);
2303 buf += len;
2304 cnt -= len;
2305 }
2306 } else
2307#endif
2308 {
2309 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002310
James Hogan34b664a2011-06-24 13:57:56 +01002311 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002312 *pdata++ = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002313 buf = pdata;
2314 }
2315 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002316 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002317 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002318 }
2319}
2320
2321static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2322{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002323 struct mmc_data *data = host->data;
2324 int init_cnt = cnt;
2325
James Hogan34b664a2011-06-24 13:57:56 +01002326 /* try and push anything in the part_buf */
2327 if (unlikely(host->part_buf_count)) {
2328 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002329
James Hogan34b664a2011-06-24 13:57:56 +01002330 buf += len;
2331 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09002332
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002333 if (host->part_buf_count == 8) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002334 mci_fifo_writeq(host->fifo_reg, host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01002335 host->part_buf_count = 0;
2336 }
2337 }
2338#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2339 if (unlikely((unsigned long)buf & 0x7)) {
2340 while (cnt >= 8) {
2341 u64 aligned_buf[16];
2342 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2343 int items = len >> 3;
2344 int i;
2345 /* memcpy from input buffer into aligned buffer */
2346 memcpy(aligned_buf, buf, len);
2347 buf += len;
2348 cnt -= len;
2349 /* push data from aligned buffer into fifo */
2350 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002351 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002352 }
2353 } else
2354#endif
2355 {
2356 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002357
James Hogan34b664a2011-06-24 13:57:56 +01002358 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002359 mci_fifo_writeq(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002360 buf = pdata;
2361 }
2362 /* put anything remaining in the part_buf */
2363 if (cnt) {
2364 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002365 /* Push data if we have reached the expected data length */
2366 if ((data->bytes_xfered + init_cnt) ==
2367 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002368 mci_fifo_writeq(host->fifo_reg, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05002369 }
2370}
2371
2372static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2373{
James Hogan34b664a2011-06-24 13:57:56 +01002374#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2375 if (unlikely((unsigned long)buf & 0x7)) {
2376 while (cnt >= 8) {
2377 /* pull data from fifo into aligned buffer */
2378 u64 aligned_buf[16];
2379 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2380 int items = len >> 3;
2381 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002382
James Hogan34b664a2011-06-24 13:57:56 +01002383 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002384 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2385
James Hogan34b664a2011-06-24 13:57:56 +01002386 /* memcpy from aligned buffer into output buffer */
2387 memcpy(buf, aligned_buf, len);
2388 buf += len;
2389 cnt -= len;
2390 }
2391 } else
2392#endif
2393 {
2394 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002395
James Hogan34b664a2011-06-24 13:57:56 +01002396 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002397 *pdata++ = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002398 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05002399 }
James Hogan34b664a2011-06-24 13:57:56 +01002400 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002401 host->part_buf = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002402 dw_mci_pull_final_bytes(host, buf, cnt);
2403 }
2404}
2405
2406static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2407{
2408 int len;
2409
2410 /* get remaining partial bytes */
2411 len = dw_mci_pull_part_bytes(host, buf, cnt);
2412 if (unlikely(len == cnt))
2413 return;
2414 buf += len;
2415 cnt -= len;
2416
2417 /* get the rest of the data */
2418 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002419}
2420
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002421static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05002422{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002423 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2424 void *buf;
2425 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002426 struct mmc_data *data = host->data;
2427 int shift = host->data_shift;
2428 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002429 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002430 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002431
2432 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002433 if (!sg_miter_next(sg_miter))
2434 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002435
Imre Deak4225fc82013-02-27 17:02:57 -08002436 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002437 buf = sg_miter->addr;
2438 remain = sg_miter->length;
2439 offset = 0;
2440
2441 do {
2442 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2443 << shift) + host->part_buf_count;
2444 len = min(remain, fcnt);
2445 if (!len)
2446 break;
2447 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002448 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002449 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002450 remain -= len;
2451 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002452
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002453 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002454 status = mci_readl(host, MINTSTS);
2455 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002456 /* if the RXDR is ready read again */
2457 } while ((status & SDMMC_INT_RXDR) ||
2458 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002459
2460 if (!remain) {
2461 if (!sg_miter_next(sg_miter))
2462 goto done;
2463 sg_miter->consumed = 0;
2464 }
2465 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002466 return;
2467
2468done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002469 sg_miter_stop(sg_miter);
2470 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002471 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002472 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2473}
2474
2475static void dw_mci_write_data_pio(struct dw_mci *host)
2476{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002477 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2478 void *buf;
2479 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002480 struct mmc_data *data = host->data;
2481 int shift = host->data_shift;
2482 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002483 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002484 unsigned int fifo_depth = host->fifo_depth;
2485 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002486
2487 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002488 if (!sg_miter_next(sg_miter))
2489 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002490
Imre Deak4225fc82013-02-27 17:02:57 -08002491 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002492 buf = sg_miter->addr;
2493 remain = sg_miter->length;
2494 offset = 0;
2495
2496 do {
2497 fcnt = ((fifo_depth -
2498 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2499 << shift) - host->part_buf_count;
2500 len = min(remain, fcnt);
2501 if (!len)
2502 break;
2503 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002504 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002505 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002506 remain -= len;
2507 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002508
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002509 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002510 status = mci_readl(host, MINTSTS);
2511 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002512 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002513
2514 if (!remain) {
2515 if (!sg_miter_next(sg_miter))
2516 goto done;
2517 sg_miter->consumed = 0;
2518 }
2519 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002520 return;
2521
2522done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002523 sg_miter_stop(sg_miter);
2524 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002525 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002526 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2527}
2528
2529static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2530{
2531 if (!host->cmd_status)
2532 host->cmd_status = status;
2533
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002534 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002535
2536 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2537 tasklet_schedule(&host->tasklet);
2538}
2539
Doug Anderson6130e7a2014-10-14 09:33:09 -07002540static void dw_mci_handle_cd(struct dw_mci *host)
2541{
2542 int i;
2543
2544 for (i = 0; i < host->num_slots; i++) {
2545 struct dw_mci_slot *slot = host->slot[i];
2546
2547 if (!slot)
2548 continue;
2549
2550 if (slot->mmc->ops->card_event)
2551 slot->mmc->ops->card_event(slot->mmc);
2552 mmc_detect_change(slot->mmc,
2553 msecs_to_jiffies(host->pdata->detect_delay_ms));
2554 }
2555}
2556
Will Newtonf95f3852011-01-02 01:11:59 -05002557static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2558{
2559 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002560 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302561 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002562
Markos Chandras1fb5f682013-03-12 10:53:11 +00002563 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2564
2565 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302566 /* Check volt switch first, since it can look like an error */
2567 if ((host->state == STATE_SENDING_CMD11) &&
2568 (pending & SDMMC_INT_VOLT_SWITCH)) {
Doug Anderson49ba0302015-04-03 11:13:07 -07002569 unsigned long irqflags;
Doug Anderson5c935162015-03-09 16:18:21 -07002570
Doug Anderson01730552014-08-22 19:17:51 +05302571 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2572 pending &= ~SDMMC_INT_VOLT_SWITCH;
Doug Anderson49ba0302015-04-03 11:13:07 -07002573
2574 /*
2575 * Hold the lock; we know cmd11_timer can't be kicked
2576 * off after the lock is released, so safe to delete.
2577 */
2578 spin_lock_irqsave(&host->irq_lock, irqflags);
Doug Anderson01730552014-08-22 19:17:51 +05302579 dw_mci_cmd_interrupt(host, pending);
Doug Anderson49ba0302015-04-03 11:13:07 -07002580 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2581
2582 del_timer(&host->cmd11_timer);
Doug Anderson01730552014-08-22 19:17:51 +05302583 }
2584
Will Newtonf95f3852011-01-02 01:11:59 -05002585 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2586 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002587 host->cmd_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002588 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002589 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002590 }
2591
2592 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2593 /* if there is an error report DATA_ERROR */
2594 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002595 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002596 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002597 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002598 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002599 }
2600
2601 if (pending & SDMMC_INT_DATA_OVER) {
Jaehoon Chung16a34572016-06-21 14:35:37 +09002602 del_timer(&host->dto_timer);
Addy Ke57e10482015-08-11 01:27:18 +09002603
Will Newtonf95f3852011-01-02 01:11:59 -05002604 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2605 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002606 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002607 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002608 if (host->dir_status == DW_MCI_RECV_STATUS) {
2609 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002610 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002611 }
2612 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2613 tasklet_schedule(&host->tasklet);
2614 }
2615
2616 if (pending & SDMMC_INT_RXDR) {
2617 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002618 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002619 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002620 }
2621
2622 if (pending & SDMMC_INT_TXDR) {
2623 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002624 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002625 dw_mci_write_data_pio(host);
2626 }
2627
2628 if (pending & SDMMC_INT_CMD_DONE) {
2629 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002630 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002631 }
2632
2633 if (pending & SDMMC_INT_CD) {
2634 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002635 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002636 }
2637
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302638 /* Handle SDIO Interrupts */
2639 for (i = 0; i < host->num_slots; i++) {
2640 struct dw_mci_slot *slot = host->slot[i];
Doug Andersoned2540e2015-02-25 10:11:52 -08002641
2642 if (!slot)
2643 continue;
2644
Addy Ke76756232014-11-04 22:03:09 +08002645 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2646 mci_writel(host, RINTSTS,
2647 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302648 mmc_signal_sdio_irq(slot->mmc);
2649 }
2650 }
2651
Markos Chandras1fb5f682013-03-12 10:53:11 +00002652 }
Will Newtonf95f3852011-01-02 01:11:59 -05002653
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002654 if (host->use_dma != TRANS_MODE_IDMAC)
2655 return IRQ_HANDLED;
2656
2657 /* Handle IDMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002658 if (host->dma_64bit_address == 1) {
2659 pending = mci_readl(host, IDSTS64);
2660 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2661 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2662 SDMMC_IDMAC_INT_RI);
2663 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002664 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2665 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002666 }
2667 } else {
2668 pending = mci_readl(host, IDSTS);
2669 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2670 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2671 SDMMC_IDMAC_INT_RI);
2672 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002673 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2674 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002675 }
Will Newtonf95f3852011-01-02 01:11:59 -05002676 }
Will Newtonf95f3852011-01-02 01:11:59 -05002677
2678 return IRQ_HANDLED;
2679}
2680
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002681static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002682{
2683 struct mmc_host *mmc;
2684 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002685 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002686 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002687 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002688
Thomas Abraham4a909202012-09-17 18:16:35 +00002689 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002690 if (!mmc)
2691 return -ENOMEM;
2692
2693 slot = mmc_priv(mmc);
2694 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002695 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002696 slot->mmc = mmc;
2697 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002698 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002699
2700 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002701 if (of_property_read_u32_array(host->dev->of_node,
2702 "clock-freq-min-max", freq, 2)) {
2703 mmc->f_min = DW_MCI_FREQ_MIN;
2704 mmc->f_max = DW_MCI_FREQ_MAX;
2705 } else {
Jaehoon Chungb0230302016-11-17 16:40:40 +09002706 dev_info(host->dev,
2707 "'clock-freq-min-max' property was deprecated.\n");
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002708 mmc->f_min = freq[0];
2709 mmc->f_max = freq[1];
2710 }
Will Newtonf95f3852011-01-02 01:11:59 -05002711
Yuvaraj CD51da2242014-08-22 19:17:50 +05302712 /*if there are external regulators, get them*/
2713 ret = mmc_regulator_get_supply(mmc);
2714 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002715 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302716
2717 if (!mmc->ocr_avail)
2718 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002719
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002720 if (host->pdata->caps)
2721 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002722
Jaehoon Chung6024e162016-07-15 10:54:50 +09002723 /*
2724 * Support MMC_CAP_ERASE by default.
2725 * It needs to use trim/discard/erase commands.
2726 */
2727 mmc->caps |= MMC_CAP_ERASE;
2728
Abhilash Kesavanab269122012-11-19 10:26:21 +05302729 if (host->pdata->pm_caps)
2730 mmc->pm_caps = host->pdata->pm_caps;
2731
Thomas Abraham800d78b2012-09-17 18:16:42 +00002732 if (host->dev->of_node) {
2733 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2734 if (ctrl_id < 0)
2735 ctrl_id = 0;
2736 } else {
2737 ctrl_id = to_platform_device(host->dev)->id;
2738 }
James Hogancb27a842012-10-16 09:43:08 +01002739 if (drv_data && drv_data->caps)
2740 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002741
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002742 if (host->pdata->caps2)
2743 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002744
Doug Anderson3cf890f2014-08-25 11:19:04 -07002745 ret = mmc_of_parse(mmc);
2746 if (ret)
2747 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002748
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002749 /* Useful defaults if platform data is unset. */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002750 if (host->use_dma == TRANS_MODE_IDMAC) {
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002751 mmc->max_segs = host->ring_size;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002752 mmc->max_blk_size = 65535;
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002753 mmc->max_seg_size = 0x1000;
2754 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2755 mmc->max_blk_count = mmc->max_req_size / 512;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002756 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2757 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002758 mmc->max_blk_size = 65535;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002759 mmc->max_blk_count = 65535;
2760 mmc->max_req_size =
2761 mmc->max_blk_size * mmc->max_blk_count;
2762 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002763 } else {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002764 /* TRANS_MODE_PIO */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002765 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002766 mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002767 mmc->max_blk_count = 512;
2768 mmc->max_req_size = mmc->max_blk_size *
2769 mmc->max_blk_count;
2770 mmc->max_seg_size = mmc->max_req_size;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002771 }
Will Newtonf95f3852011-01-02 01:11:59 -05002772
Shawn Linc0834a52016-05-27 14:36:40 +08002773 dw_mci_get_cd(mmc);
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002774
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002775 ret = mmc_add_host(mmc);
2776 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002777 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002778
2779#if defined(CONFIG_DEBUG_FS)
2780 dw_mci_init_debugfs(slot);
2781#endif
2782
Will Newtonf95f3852011-01-02 01:11:59 -05002783 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002784
Doug Anderson3cf890f2014-08-25 11:19:04 -07002785err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002786 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302787 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002788}
2789
2790static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2791{
Will Newtonf95f3852011-01-02 01:11:59 -05002792 /* Debugfs stuff is cleaned up by mmc core */
2793 mmc_remove_host(slot->mmc);
2794 slot->host->slot[id] = NULL;
2795 mmc_free_host(slot->mmc);
2796}
2797
2798static void dw_mci_init_dma(struct dw_mci *host)
2799{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002800 int addr_config;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002801 struct device *dev = host->dev;
2802 struct device_node *np = dev->of_node;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002803
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002804 /*
2805 * Check tansfer mode from HCON[17:16]
2806 * Clear the ambiguous description of dw_mmc databook:
2807 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2808 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2809 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2810 * 2b'11: Non DW DMA Interface -> pio only
2811 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2812 * simpler request/acknowledge handshake mechanism and both of them
2813 * are regarded as external dma master for dw_mmc.
2814 */
2815 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2816 if (host->use_dma == DMA_INTERFACE_IDMA) {
2817 host->use_dma = TRANS_MODE_IDMAC;
2818 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2819 host->use_dma == DMA_INTERFACE_GDMA) {
2820 host->use_dma = TRANS_MODE_EDMAC;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002821 } else {
Will Newtonf95f3852011-01-02 01:11:59 -05002822 goto no_dma;
2823 }
2824
2825 /* Determine which DMA interface to use */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002826 if (host->use_dma == TRANS_MODE_IDMAC) {
2827 /*
2828 * Check ADDR_CONFIG bit in HCON to find
2829 * IDMAC address bus width
2830 */
Shawn Lin70692752015-09-16 14:41:37 +08002831 addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05002832
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002833 if (addr_config == 1) {
2834 /* host supports IDMAC in 64-bit address mode */
2835 host->dma_64bit_address = 1;
2836 dev_info(host->dev,
2837 "IDMAC supports 64-bit address mode.\n");
2838 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2839 dma_set_coherent_mask(host->dev,
2840 DMA_BIT_MASK(64));
2841 } else {
2842 /* host supports IDMAC in 32-bit address mode */
2843 host->dma_64bit_address = 0;
2844 dev_info(host->dev,
2845 "IDMAC supports 32-bit address mode.\n");
2846 }
2847
2848 /* Alloc memory for sg translation */
Shawn Lincc190d42016-09-02 12:14:39 +08002849 host->sg_cpu = dmam_alloc_coherent(host->dev,
2850 DESC_RING_BUF_SZ,
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002851 &host->sg_dma, GFP_KERNEL);
2852 if (!host->sg_cpu) {
2853 dev_err(host->dev,
2854 "%s: could not alloc DMA memory\n",
2855 __func__);
2856 goto no_dma;
2857 }
2858
2859 host->dma_ops = &dw_mci_idmac_ops;
2860 dev_info(host->dev, "Using internal DMA controller.\n");
2861 } else {
2862 /* TRANS_MODE_EDMAC: check dma bindings again */
2863 if ((of_property_count_strings(np, "dma-names") < 0) ||
2864 (!of_find_property(np, "dmas", NULL))) {
2865 goto no_dma;
2866 }
2867 host->dma_ops = &dw_mci_edmac_ops;
2868 dev_info(host->dev, "Using external DMA controller.\n");
2869 }
Will Newtonf95f3852011-01-02 01:11:59 -05002870
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002871 if (host->dma_ops->init && host->dma_ops->start &&
2872 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002873 if (host->dma_ops->init(host)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002874 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2875 __func__);
Will Newtonf95f3852011-01-02 01:11:59 -05002876 goto no_dma;
2877 }
2878 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002879 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002880 goto no_dma;
2881 }
2882
Will Newtonf95f3852011-01-02 01:11:59 -05002883 return;
2884
2885no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002886 dev_info(host->dev, "Using PIO mode.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002887 host->use_dma = TRANS_MODE_PIO;
Will Newtonf95f3852011-01-02 01:11:59 -05002888}
2889
Seungwon Jeon31bff452013-08-31 00:14:23 +09002890static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002891{
Seungwon Jeon31bff452013-08-31 00:14:23 +09002892 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002893
Seungwon Jeon31bff452013-08-31 00:14:23 +09002894 ctrl = mci_readl(host, CTRL);
2895 ctrl |= reset;
2896 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002897
2898 /* wait till resets clear */
Shawn Linb6d2d812017-02-17 10:56:39 +08002899 if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
2900 !(ctrl & reset),
2901 1, 500 * USEC_PER_MSEC)) {
2902 dev_err(host->dev,
2903 "Timeout resetting block (ctrl reset %#x)\n",
2904 ctrl & reset);
2905 return false;
2906 }
Will Newtonf95f3852011-01-02 01:11:59 -05002907
Shawn Linb6d2d812017-02-17 10:56:39 +08002908 return true;
Will Newtonf95f3852011-01-02 01:11:59 -05002909}
2910
Doug Anderson5c935162015-03-09 16:18:21 -07002911static void dw_mci_cmd11_timer(unsigned long arg)
2912{
2913 struct dw_mci *host = (struct dw_mci *)arg;
2914
Doug Andersonfd674192015-04-03 11:13:06 -07002915 if (host->state != STATE_SENDING_CMD11) {
2916 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2917 return;
2918 }
Doug Anderson5c935162015-03-09 16:18:21 -07002919
2920 host->cmd_status = SDMMC_INT_RTO;
2921 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2922 tasklet_schedule(&host->tasklet);
2923}
2924
Addy Ke57e10482015-08-11 01:27:18 +09002925static void dw_mci_dto_timer(unsigned long arg)
2926{
2927 struct dw_mci *host = (struct dw_mci *)arg;
2928
2929 switch (host->state) {
2930 case STATE_SENDING_DATA:
2931 case STATE_DATA_BUSY:
2932 /*
2933 * If DTO interrupt does NOT come in sending data state,
2934 * we should notify the driver to terminate current transfer
2935 * and report a data timeout to the core.
2936 */
2937 host->data_status = SDMMC_INT_DRTO;
2938 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2939 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2940 tasklet_schedule(&host->tasklet);
2941 break;
2942 default:
2943 break;
2944 }
2945}
2946
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002947#ifdef CONFIG_OF
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002948static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2949{
2950 struct dw_mci_board *pdata;
2951 struct device *dev = host->dev;
2952 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002953 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Line8cc37b2016-01-21 14:52:52 +08002954 int ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002955 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002956
2957 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +09002958 if (!pdata)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002959 return ERR_PTR(-ENOMEM);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002960
Guodong Xud6786fe2016-08-12 16:51:26 +08002961 /* find reset controller when exist */
Jaehoon Chung3a667e32016-10-31 11:49:42 +09002962 pdata->rstc = devm_reset_control_get_optional(dev, "reset");
Guodong Xud6786fe2016-08-12 16:51:26 +08002963 if (IS_ERR(pdata->rstc)) {
2964 if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
2965 return ERR_PTR(-EPROBE_DEFER);
2966 }
2967
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002968 /* find out number of slots supported */
Shawn Lin8a629d22016-02-02 14:11:25 +08002969 of_property_read_u32(np, "num-slots", &pdata->num_slots);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002970
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002971 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002972 dev_info(dev,
2973 "fifo-depth property not found, using value of FIFOTH register as default\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002974
2975 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2976
Jun Niea0361c12017-01-11 15:35:35 +09002977 of_property_read_u32(np, "data-addr", &host->data_addr_override);
2978
Jun Nied6fced82017-01-11 15:37:26 +09002979 if (of_get_property(np, "fifo-watermark-aligned", NULL))
2980 host->wm_aligned = true;
2981
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002982 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2983 pdata->bus_hz = clock_frequency;
2984
James Hogancb27a842012-10-16 09:43:08 +01002985 if (drv_data && drv_data->parse_dt) {
2986 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002987 if (ret)
2988 return ERR_PTR(ret);
2989 }
2990
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002991 return pdata;
2992}
2993
2994#else /* CONFIG_OF */
2995static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2996{
2997 return ERR_PTR(-EINVAL);
2998}
2999#endif /* CONFIG_OF */
3000
Doug Andersonfa0c3282015-02-25 10:11:51 -08003001static void dw_mci_enable_cd(struct dw_mci *host)
3002{
Doug Andersonfa0c3282015-02-25 10:11:51 -08003003 unsigned long irqflags;
3004 u32 temp;
3005 int i;
Shawn Line8cc37b2016-01-21 14:52:52 +08003006 struct dw_mci_slot *slot;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003007
Shawn Line8cc37b2016-01-21 14:52:52 +08003008 /*
3009 * No need for CD if all slots have a non-error GPIO
3010 * as well as broken card detection is found.
3011 */
Doug Andersonfa0c3282015-02-25 10:11:51 -08003012 for (i = 0; i < host->num_slots; i++) {
Shawn Line8cc37b2016-01-21 14:52:52 +08003013 slot = host->slot[i];
3014 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3015 return;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003016
Arnd Bergmann287980e2016-05-27 23:23:25 +02003017 if (mmc_gpio_get_cd(slot->mmc) < 0)
Doug Andersonfa0c3282015-02-25 10:11:51 -08003018 break;
3019 }
3020 if (i == host->num_slots)
3021 return;
3022
3023 spin_lock_irqsave(&host->irq_lock, irqflags);
3024 temp = mci_readl(host, INTMASK);
3025 temp |= SDMMC_INT_CD;
3026 mci_writel(host, INTMASK, temp);
3027 spin_unlock_irqrestore(&host->irq_lock, irqflags);
3028}
3029
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303030int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003031{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00003032 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303033 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003034 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003035 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003036
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003037 if (!host->pdata) {
3038 host->pdata = dw_mci_parse_dt(host);
Guodong Xud6786fe2016-08-12 16:51:26 +08003039 if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3040 return -EPROBE_DEFER;
3041 } else if (IS_ERR(host->pdata)) {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003042 dev_err(host->dev, "platform data not available\n");
3043 return -EINVAL;
3044 }
Will Newtonf95f3852011-01-02 01:11:59 -05003045 }
3046
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003047 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003048 if (IS_ERR(host->biu_clk)) {
3049 dev_dbg(host->dev, "biu clock not available\n");
3050 } else {
3051 ret = clk_prepare_enable(host->biu_clk);
3052 if (ret) {
3053 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003054 return ret;
3055 }
Will Newtonf95f3852011-01-02 01:11:59 -05003056 }
3057
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003058 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003059 if (IS_ERR(host->ciu_clk)) {
3060 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003061 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003062 } else {
3063 ret = clk_prepare_enable(host->ciu_clk);
3064 if (ret) {
3065 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003066 goto err_clk_biu;
3067 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003068
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003069 if (host->pdata->bus_hz) {
3070 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3071 if (ret)
3072 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003073 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003074 host->pdata->bus_hz);
3075 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003076 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003077 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003078
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003079 if (!host->bus_hz) {
3080 dev_err(host->dev,
3081 "Platform data must supply bus speed\n");
3082 ret = -ENODEV;
3083 goto err_clk_ciu;
3084 }
3085
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09003086 if (drv_data && drv_data->init) {
3087 ret = drv_data->init(host);
3088 if (ret) {
3089 dev_err(host->dev,
3090 "implementation specific init failed\n");
3091 goto err_clk_ciu;
3092 }
3093 }
3094
Guodong Xud6786fe2016-08-12 16:51:26 +08003095 if (!IS_ERR(host->pdata->rstc)) {
3096 reset_control_assert(host->pdata->rstc);
3097 usleep_range(10, 50);
3098 reset_control_deassert(host->pdata->rstc);
3099 }
3100
Doug Anderson5c935162015-03-09 16:18:21 -07003101 setup_timer(&host->cmd11_timer,
3102 dw_mci_cmd11_timer, (unsigned long)host);
3103
Jaehoon Chung16a34572016-06-21 14:35:37 +09003104 setup_timer(&host->dto_timer,
3105 dw_mci_dto_timer, (unsigned long)host);
Addy Ke57e10482015-08-11 01:27:18 +09003106
Will Newtonf95f3852011-01-02 01:11:59 -05003107 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08003108 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05003109 INIT_LIST_HEAD(&host->queue);
3110
Will Newtonf95f3852011-01-02 01:11:59 -05003111 /*
3112 * Get the host data width - this assumes that HCON has been set with
3113 * the correct values.
3114 */
Shawn Lin70692752015-09-16 14:41:37 +08003115 i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05003116 if (!i) {
3117 host->push_data = dw_mci_push_data16;
3118 host->pull_data = dw_mci_pull_data16;
3119 width = 16;
3120 host->data_shift = 1;
3121 } else if (i == 2) {
3122 host->push_data = dw_mci_push_data64;
3123 host->pull_data = dw_mci_pull_data64;
3124 width = 64;
3125 host->data_shift = 3;
3126 } else {
3127 /* Check for a reserved value, and warn if it is */
3128 WARN((i != 1),
3129 "HCON reports a reserved host data width!\n"
3130 "Defaulting to 32-bit access.\n");
3131 host->push_data = dw_mci_push_data32;
3132 host->pull_data = dw_mci_pull_data32;
3133 width = 32;
3134 host->data_shift = 2;
3135 }
3136
3137 /* Reset all blocks */
Shawn Lin37444152016-01-22 15:43:12 +08003138 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3139 ret = -ENODEV;
3140 goto err_clk_ciu;
3141 }
Seungwon Jeon141a7122012-05-22 13:01:03 +09003142
3143 host->dma_ops = host->pdata->dma_ops;
3144 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05003145
3146 /* Clear the interrupts for the host controller */
3147 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3148 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3149
3150 /* Put in max timeout */
3151 mci_writel(host, TMOUT, 0xFFFFFFFF);
3152
3153 /*
3154 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3155 * Tx Mark = fifo_size / 2 DMA Size = 8
3156 */
James Hoganb86d8252011-06-24 13:57:18 +01003157 if (!host->pdata->fifo_depth) {
3158 /*
3159 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3160 * have been overwritten by the bootloader, just like we're
3161 * about to do, so if you know the value for your hardware, you
3162 * should put it in the platform data.
3163 */
3164 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00003165 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01003166 } else {
3167 fifo_size = host->pdata->fifo_depth;
3168 }
3169 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09003170 host->fifoth_val =
3171 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003172 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05003173
3174 /* disable clock to CIU */
3175 mci_writel(host, CLKENA, 0);
3176 mci_writel(host, CLKSRC, 0);
3177
James Hogan63008762013-03-12 10:43:54 +00003178 /*
3179 * In 2.40a spec, Data offset is changed.
3180 * Need to check the version-id and set data-offset for DATA register.
3181 */
3182 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3183 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3184
Jun Niea0361c12017-01-11 15:35:35 +09003185 if (host->data_addr_override)
3186 host->fifo_reg = host->regs + host->data_addr_override;
3187 else if (host->verid < DW_MMC_240A)
Ben Dooks76184ac2015-03-25 11:27:52 +00003188 host->fifo_reg = host->regs + DATA_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003189 else
Ben Dooks76184ac2015-03-25 11:27:52 +00003190 host->fifo_reg = host->regs + DATA_240A_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003191
Will Newtonf95f3852011-01-02 01:11:59 -05003192 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003193 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3194 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05003195 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07003196 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003197
Will Newtonf95f3852011-01-02 01:11:59 -05003198 if (host->pdata->num_slots)
3199 host->num_slots = host->pdata->num_slots;
3200 else
Shawn Lin8a629d22016-02-02 14:11:25 +08003201 host->num_slots = 1;
3202
3203 if (host->num_slots < 1 ||
3204 host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) {
3205 dev_err(host->dev,
3206 "Platform data must supply correct num_slots.\n");
3207 ret = -ENODEV;
3208 goto err_clk_ciu;
3209 }
Will Newtonf95f3852011-01-02 01:11:59 -05003210
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303211 /*
Doug Andersonfa0c3282015-02-25 10:11:51 -08003212 * Enable interrupts for command done, data over, data empty,
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303213 * receive ready and error such as transmit, receive timeout, crc error
3214 */
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303215 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3216 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003217 DW_MCI_ERROR_FLAGS);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003218 /* Enable mci interrupt */
3219 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303220
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003221 dev_info(host->dev,
3222 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303223 host->irq, width, fifo_size);
3224
Will Newtonf95f3852011-01-02 01:11:59 -05003225 /* We need at least one slot to succeed */
3226 for (i = 0; i < host->num_slots; i++) {
3227 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003228 if (ret)
3229 dev_dbg(host->dev, "slot %d init failed\n", i);
3230 else
3231 init_slots++;
3232 }
3233
3234 if (init_slots) {
3235 dev_info(host->dev, "%d slots initialized\n", init_slots);
3236 } else {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003237 dev_dbg(host->dev,
3238 "attempted to initialize %d slots, but failed on all\n",
3239 host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07003240 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003241 }
3242
Doug Andersonb793f652015-03-11 15:15:14 -07003243 /* Now that slots are all setup, we can enable card detect */
3244 dw_mci_enable_cd(host);
3245
Will Newtonf95f3852011-01-02 01:11:59 -05003246 return 0;
3247
Will Newtonf95f3852011-01-02 01:11:59 -05003248err_dmaunmap:
3249 if (host->use_dma && host->dma_ops->exit)
3250 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003251
Guodong Xud6786fe2016-08-12 16:51:26 +08003252 if (!IS_ERR(host->pdata->rstc))
3253 reset_control_assert(host->pdata->rstc);
3254
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003255err_clk_ciu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003256 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003257
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003258err_clk_biu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003259 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003260
Will Newtonf95f3852011-01-02 01:11:59 -05003261 return ret;
3262}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303263EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05003264
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303265void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003266{
Will Newtonf95f3852011-01-02 01:11:59 -05003267 int i;
3268
Will Newtonf95f3852011-01-02 01:11:59 -05003269 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00003270 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05003271 if (host->slot[i])
3272 dw_mci_cleanup_slot(host->slot[i], i);
3273 }
3274
Prabu Thangamuthu048fd7e2015-05-28 12:21:06 +00003275 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3276 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3277
Will Newtonf95f3852011-01-02 01:11:59 -05003278 /* disable clock to CIU */
3279 mci_writel(host, CLKENA, 0);
3280 mci_writel(host, CLKSRC, 0);
3281
Will Newtonf95f3852011-01-02 01:11:59 -05003282 if (host->use_dma && host->dma_ops->exit)
3283 host->dma_ops->exit(host);
3284
Guodong Xud6786fe2016-08-12 16:51:26 +08003285 if (!IS_ERR(host->pdata->rstc))
3286 reset_control_assert(host->pdata->rstc);
3287
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003288 clk_disable_unprepare(host->ciu_clk);
3289 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05003290}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303291EXPORT_SYMBOL(dw_mci_remove);
3292
3293
Will Newtonf95f3852011-01-02 01:11:59 -05003294
Shawn Line9ed8832016-10-12 10:50:35 +08003295#ifdef CONFIG_PM
Shawn Lined24e1f2016-10-12 10:56:55 +08003296int dw_mci_runtime_suspend(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003297{
Shawn Lined24e1f2016-10-12 10:56:55 +08003298 struct dw_mci *host = dev_get_drvdata(dev);
3299
Shawn Lin3fc7eae2015-09-16 14:41:23 +08003300 if (host->use_dma && host->dma_ops->exit)
3301 host->dma_ops->exit(host);
3302
Shawn Lined24e1f2016-10-12 10:56:55 +08003303 clk_disable_unprepare(host->ciu_clk);
3304
3305 if (host->cur_slot &&
3306 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3307 !mmc_card_is_removable(host->cur_slot->mmc)))
3308 clk_disable_unprepare(host->biu_clk);
3309
Will Newtonf95f3852011-01-02 01:11:59 -05003310 return 0;
3311}
Shawn Lined24e1f2016-10-12 10:56:55 +08003312EXPORT_SYMBOL(dw_mci_runtime_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05003313
Shawn Lined24e1f2016-10-12 10:56:55 +08003314int dw_mci_runtime_resume(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003315{
Shawn Lined24e1f2016-10-12 10:56:55 +08003316 int i, ret = 0;
3317 struct dw_mci *host = dev_get_drvdata(dev);
Will Newtonf95f3852011-01-02 01:11:59 -05003318
Shawn Lined24e1f2016-10-12 10:56:55 +08003319 if (host->cur_slot &&
3320 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3321 !mmc_card_is_removable(host->cur_slot->mmc))) {
3322 ret = clk_prepare_enable(host->biu_clk);
3323 if (ret)
3324 return ret;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003325 }
3326
Shawn Lined24e1f2016-10-12 10:56:55 +08003327 ret = clk_prepare_enable(host->ciu_clk);
3328 if (ret)
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003329 goto err;
3330
3331 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3332 clk_disable_unprepare(host->ciu_clk);
3333 ret = -ENODEV;
3334 goto err;
3335 }
Shawn Lined24e1f2016-10-12 10:56:55 +08003336
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04003337 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09003338 host->dma_ops->init(host);
3339
Seungwon Jeon52426892013-08-31 00:13:42 +09003340 /*
3341 * Restore the initial value at FIFOTH register
3342 * And Invalidate the prev_blksz with zero
3343 */
Shawn Lined24e1f2016-10-12 10:56:55 +08003344 mci_writel(host, FIFOTH, host->fifoth_val);
3345 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003346
Doug Anderson2eb29442013-08-31 00:11:49 +09003347 /* Put in max timeout */
3348 mci_writel(host, TMOUT, 0xFFFFFFFF);
3349
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003350 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3351 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3352 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003353 DW_MCI_ERROR_FLAGS);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003354 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3355
Will Newtonf95f3852011-01-02 01:11:59 -05003356 for (i = 0; i < host->num_slots; i++) {
3357 struct dw_mci_slot *slot = host->slot[i];
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003358
Will Newtonf95f3852011-01-02 01:11:59 -05003359 if (!slot)
3360 continue;
Ziyuan Xue9748e02017-01-17 09:22:56 +08003361 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
Abhilash Kesavanab269122012-11-19 10:26:21 +05303362 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
Ziyuan Xue9748e02017-01-17 09:22:56 +08003363
3364 /* Force setup bus to guarantee available clock output */
3365 dw_mci_setup_bus(slot, true);
Will Newtonf95f3852011-01-02 01:11:59 -05003366 }
Doug Andersonfa0c3282015-02-25 10:11:51 -08003367
3368 /* Now that slots are all setup, we can enable card detect */
3369 dw_mci_enable_cd(host);
3370
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003371 return 0;
3372
3373err:
3374 if (host->cur_slot &&
3375 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3376 !mmc_card_is_removable(host->cur_slot->mmc)))
3377 clk_disable_unprepare(host->biu_clk);
3378
Shawn Lined24e1f2016-10-12 10:56:55 +08003379 return ret;
Shawn Line9ed8832016-10-12 10:50:35 +08003380}
3381EXPORT_SYMBOL(dw_mci_runtime_resume);
3382#endif /* CONFIG_PM */
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003383
Will Newtonf95f3852011-01-02 01:11:59 -05003384static int __init dw_mci_init(void)
3385{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05303386 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303387 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003388}
3389
3390static void __exit dw_mci_exit(void)
3391{
Will Newtonf95f3852011-01-02 01:11:59 -05003392}
3393
3394module_init(dw_mci_init);
3395module_exit(dw_mci_exit);
3396
3397MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3398MODULE_AUTHOR("NXP Semiconductor VietNam");
3399MODULE_AUTHOR("Imagination Technologies Ltd");
3400MODULE_LICENSE("GPL v2");